原题:http://acm.hdu.edu.cn/showproblem.php?pid=4751
题意:
有n个人;
下面n行,表示第i个人所认识的人,以0结束;
问能否分成两个集合,使得每个集合均为完全图;
思路:
也就是说不认识的人一定不在一个集合里;
用交叉染色法来判断即可;
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 110;
int n;
bool link[maxn][maxn];
int map[maxn];
bool bfs(int x)
{
queue<int>Q;
Q.push(x);
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i = 1;i<=n;i++)
{
if(i == u || (link[u][i] && link[i][u])) continue;
if(map[i] == -1)
{
map[i] = map[u]^1;
Q.push(i);
}
else if(map[u] == map[i])
return false;
}
}
return true;
}
int main()
{
while(scanf("%d", &n)!=EOF)
{
memset(link, false, sizeof link);
memset(map, -1, sizeof map);
for(int i = 1;i<=n;i++)
{
int x;
scanf("%d", &x);
while(x!=0)
{
link[i][x] = true;
scanf("%d", &x);
}
}
int i;
for(i = 1;i<=n;i++)
{
if(map[i] == -1)
{
map[i] = 0;
if(!bfs(i))
break;
}
}
if(i>n) printf("YES\n");
else printf("NO\n");
}
return 0;
}