2015
09-17

# 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.

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.

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.

3
3 0
1 0
1 2 0

YES

#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=110;
int e[maxn][maxn];
int vis[maxn],n;
int bfs(int x)//从x点开始分组。
{
queue<int>q;
q.push(x);
vis[x]=1;
int i,j,k,u,v;
while(!q.empty())
{
u=q.front();
q.pop();
for(i=1;i<=n;i++)
{
if(e[u][i]==1||u==i)continue;
if(vis[i]==-1)
{
vis[i]=1-vis[u];
q.push(i);
}
else if(vis[i]==vis[u])return 0;
}
}
return 1;
}
int main()
{
while(cin>>n)
{
int i,j,k,a;
memset(e,0,sizeof(e));
memset(vis,-1,sizeof(vis));
for(i=1;i<=n;i++)
{
while(cin>>a&&a!=0)
e[i][a]=1;
}
//改成无向图
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(!e[i][j])e[j][i]=0;
for(i=1;i<=n;i++)
{
if(vis[i]!=-1)continue;
if(!bfs(i))break;
}
if(i<=n)cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return 0;
}
/*
题意：将n个人分成两组，使得每组中的每个人都认识剩余所有人。
用二维数组e记录关系，e[i][j]==0表示i不认识j。
vis[i]表示组别，分别为1和0。则可知若e[i][j]==0 ,则i和j必定是一个是0，一个是1。
bfs一遍，当出现矛盾的时候就不能分组。
由于枚举所有点作起点，且起点为1组内。又e[i][j]=0,e[j][i]=1和e[i][j]=e[j][i]=0情况相同。

*/

, ,
1. 你觉得你是猪，我可没觉得我是啊，不要把我们正常人跟你这个脑残相提并论，自己是猪就呆在猪圈里，跑出来在这里喷粪干吗。