欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU4751Divide Groups

程序员文章站 2022-05-22 13:37:15
...

Divide Groups

 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.

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.

Sample Input

3 3 0 1 0 1 2 0

Sample Output

YES

给你n个人,让你分成2堆人且两堆人中的人都互相认识(a认识b,但b不认识a,不能说a与b认识)即形成完全图(完全图:图的每两个顶点之间有边链接),完全图的补图是二分图,我们可以通过判断它的补图是否是二分图来判断它是否是完全图

                                                                            HDU4751Divide Groups

                                                                                          完全图 

#include<iostream>
#include<vector>
#include<cstring>
#define N 105
using namespace std;
vector<int>G[N];
int color[N];
int vis[N][N];
void init(int n)
{
	memset(color,0,sizeof(color));
	memset(vis,0,sizeof(vis));
	for(int i=0;i<=n;i++)
	G[i].clear();
}

bool dfs(int pos,int cnt)
{
	color[pos]=cnt;
	for(int i=0;i<G[pos].size();i++)
	{
		if(color[G[pos][i]]==cnt)
		{
			return false;
		}
		if(color[G[pos][i]]==0&&!dfs(G[pos][i],-cnt))
		{
			return false;
		}
	}
	return true;
}

int main()
{
	int n,x;
	while(scanf("%d",&n)!=EOF)
	{
		init(n);
		for(int i=1;i<=n;i++)
		{
			while(scanf("%d",&x)&&x)
			{
				vis[i][x]=1;
			}
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=i+1;j<=n;j++)
			{
				if(!vis[i][j]||!vis[j][i])
				{
					G[i].push_back(j);
					G[j].push_back(i);
				}
			}
		}
		int flag=1;
		for(int i=1;i<=n;i++)
		{
			if(color[i]==0)
			{
				if(!dfs(i,1))
				{
					flag=0;
					break;
				}
			}
		}
		if(flag)
		printf("YES\n");
		else
		printf("NO\n");
	}
}