http://poj.org/problem?id=2492
搞基题啊我操,给的就是n个虫子,m对虫子发生过关系 然后问有没有虫子是同性恋。。。
并查集,需要用t数组保存x对f[x]的关系,0为同性,1为异性。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const double eps=1e-8;
const double INF=1e50;
//const double pi=acos(-1);
#define N 2005
#define M 1000005
int f[N],n,m,t[N];
int find(int x)
{
if (f[x]==x) return x;
int fx=f[x];
f[x]=find(f[x]);
t[x]=(t[x]+t[fx])%2;//这里不可t[x]=(t[x]+t[f[x]])%2
return f[x];
}
int main()
{
freopen("a","r",stdin);
int i,k,T,a,b;
scanf("%d",&T);
for (int kk=1;kk<=T;kk++)
{
if (kk>1) printf("\n");
printf("Scenario #%d:\n",kk);
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++)
{
f[i]=i;
t[i]=0;
}
bool flag=true;
for (k=1;k<=m;k++)
{
scanf("%d%d",&a,&b);
int fa=find(a),fb=find(b);
if (fa==fb)
{
if (t[a]==t[b]) flag=false;
}
else
{
f[fb]=fa;
t[fb]=(t[b]+t[a]+1)%2;
}
}
if (flag==true) printf("No suspicious bugs found!\n");
else printf("Suspicious bugs found!\n");
}
return 0;
}