码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4751 Divide Groups(二分图的判断)

时间:2015-03-20 09:24:12      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:二分图

Problem Description
技术分享

  This year is the 60th anniversary of NJUST, and to make the celebration more colorful, Tom200 is going to invite distinguished alumnus back to visit and take photos.
  After carefully planning, Tom200 announced his activity plan, one that contains two characters:
  1. Whether the effect of the event are good or bad has nothing to do with the number of people join in.
  2. The more people joining in one activity know each other, the more interesting the activity will be. Therefore, the best state is that, everyone knows each other.
  The event appeals to a great number of alumnus, and Tom200 finds that they may not know each other or may just unilaterally recognize others. To improve the activities effects, Tom200 has to divide all those who signed up into groups to take part in the activity at different time. As we know, one‘s energy is limited, and Tom200 can hold activity twice. Tom200 already knows the relationship of each two person, but he cannot divide them because the number is too large.
  Now Tom200 turns to you for help. Given the information, can you tell if it is possible to complete the dividing mission to make the two activity in best state.
 

Input
  The input contains several test cases, terminated by EOF.
  Each case starts with a positive integer n (2<=n<=100), which means the number of people joining in the event.
  N lines follow. The i-th line contains some integers which are the id
of students that the i-th student knows, terminated by 0. And the id starts from 1.
 

Output
  If divided successfully, please output "YES" in a line, else output "NO".
 

Sample Input
3 3 0 1 0 1 2 0
 

Sample Output
YES

题意:n个人分成两个集合,要每一个图的认识关系都是完全图,是否可以分成

思路:先把不是相互认识的放在一个连接表联系起来,这些人必须不是一个集合的,然后染色法判断




#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>


#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef __int64 ll;

#define fre(i,a,b)  for(i = a; i < b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define ssf(n)      scanf("%s", n)
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 0x3f3f3f3f
#define N 105

vector<int>g[N];
int n,vis[N];
int a[N][N];

bool dfs(int x,int color)
{
     int i;
     vis[x]=color;
     fre(i,0,g[x].size())
	{
        int to=g[x][i];

        if(vis[to]!=-1)
		{
			if(vis[to]==color) return false;  //不认识但是在一个集合
			continue;
		}

        if(!dfs(to,!color))  //不认识就得在不同的集合
		  return false;
	}
   return true;
}

bool solve()
{
	int i,j;
	mem(vis,-1);

	fre(i,1,n+1)
	{
		if(vis[i]==-1&&dfs(i,0)==false)
			return false;
	}
   return true;

}

int main()
{
	int i,j;
	while(~sf(n))
	{
		fre(i,1,n+1)
		  g[i].clear();

		int x;
		mem(a,0);
		fre(i,1,n+1)
		{
			while(sf(x),x)
				{
				  a[i][x]=1;
				}
		}

		fre(i,1,n+1)
		  fre(j,i+1,n+1)
		   if(a[i][j]==0||a[j][i]==0)
		   {
		   	  g[i].push_back(j);
		   	  g[j].push_back(i);
		   }

	   if(solve())
		pf("YES\n");
	   else
		pf("NO\n");
	}
     return 0;
}








HDU 4751 Divide Groups(二分图的判断)

标签:二分图

原文地址:http://blog.csdn.net/u014737310/article/details/44467523

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!