hdu1829 这是一道比较简单 好理解的并查集
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <string.h>
#include <map>
#include <set>
using namespace std;
int f[2001],r[2001];
int n,k;
int findfa(int x)
{
if(x==f[x])
{
return x;
}
else
{
int t=f[x];
f[x]=findfa(f[x]);
r[x]=(r[x]+r[t]+2)%2;
return f[x];
}
}
void bing(int x,int y)
{
int tx=findfa(x),ty=findfa(y);
if(tx!=ty)
{
f[ty]=tx;
r[ty]=(r[x]-r[y]+1+2*2)%2;
}
}
int istrue(int x,int y)
{
if(x>n||y>n||x==y)
{
return 0;
}
int tx=findfa(x),ty=findfa(y);
if(tx!=ty)return 1;
else
{
if(r[x]==(r[y]+1)%2)
{
return 1;
}
else return 0;
}
}
int main()
{
int i,t,ans,tt=1,x,y;
scanf("%d",&t);
while(t--)
{
for(i=1;i<=2001;i++)
{
f[i]=i;r[i]=0;
}
scanf("%d%d",&n,&k);
ans=0;
for(i=1;i<=k;i++)
{
scanf("%d%d",&x,&y);
if(istrue(x,y))
{
bing(x,y);
}
else ans++;
}
printf("Scenario #%d:\n",tt++);
if(ans!=0)printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
printf("\n");
}
return 0;
}