二分图的判定问题... 利用二分图的一个性质X(Y)点集内的任意两点是不存在边的,所以建立补图一个二分图的判定就可以搞定了。。。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 110;
bool map[MAXN][MAXN];
int color[MAXN];
vector<int> G[MAXN];
int n;
bool Check(int u, int col)
{
color[u] = col;
for(int i = 0; i < (int)G[u].size(); ++i)
{
int v = G[u][i];
if(color[v] == color[u])
return false;
if(!color[v])
{
if(!Check(v, 3 - col))
return false;
}
}
return true;
}
int main()
{
while(scanf("%d", &n) == 1)
{
for(int i = 0; i <= n; ++i)
G[i].clear();
memset(map, true, sizeof(map));
memset(color, 0, sizeof(color));
for(int u = 1; u <= n; ++u)
{
int v;
while(true)
{
scanf("%d", &v);
if(v == 0)
break;
map[u][v] = false;
}
}
for(int i = 1; i <= n; ++i)
{
for(int j = i + 1; j <= n; ++j)
{
if(map[i][j] || map[j][i])
{
G[i].push_back(j);
G[j].push_back(i);
}
}
}
bool flag = true;
for(int i = 1; i <= n; ++i)
{
if(!color[i])
{
if(!Check(i, 1))
{
flag = false;
break;
}
}
}
if(flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}