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
Source
2013 ACM/ICPC Asia Regional Nanjing Online
给出 n 个人以及相互之间的关系,问是否能分成两组,且组内的人互相认识;
把互不认识的人之间建边,然后染色判断是否为标准的二分图就好了~~
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
int mapp[112][112],pa[112];
vector <int> v[112];
int N;
bool flag;
void init(){
int i,j;
memset(pa,-1,sizeof(pa));
for(i = 0 ; i <= N ; i++)
v[i].clear();
for(i = 1 ; i <= N ; i++)
for(j = 1 ; j <= N ; j++)
mapp[i][j] = 1;
}
void sol(int x)
{
int y,i;
for(i = 0 ; i < v[x].size();i++)
{
y = v[x][i];
if(pa[x] == pa[y]) // 如果出现了相同的则分不成两派
flag = false;
if(pa[y] != -1)
continue;
pa[y] = pa[x] ^ 1; // 分成两派
sol(y);
}
}
void solve()
{
for(int i = 1 ; i <= N ; i++)
{
if(pa[i] == -1)
{
pa[i] = 0;
sol(i);
}
}
}
int main()
{
int a,i,j;
while(scanf("%d",&N)!=EOF && N)
{
init();
for(i = 1 ; i <= N ; i++)
while(scanf("%d",&a) && a)
mapp[i][a] = 0;
for(i = 1 ; i <= N ; i++)
for(j = 1 ; j <= N ; j++)
if(mapp[i][j] && i != j){ //如果互不认识建边
v[i].push_back(j);
v[j].push_back(i);
}
flag = true;
solve();
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}