链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829
题意:有n只虫子,和m对性别不同的情侣,判断是否出现性别矛盾
思路:如果你做过poj1182食物链就会发现这题简单的不行!!
和poj1182一样的思路,具体可详见那题的题解:http://blog.csdn.net/qq_36782366/article/details/75014486
主要是处理关系上的方法 运用了类似于关系有向相加的公式
直接上代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 2000
struct node
{
int pre;
int relation;
}p[1000000];
int n,m;
int find(int x)
{
int r;
if(x==p[x].pre)
{
return x;
}
r=p[x].pre;
p[x].pre=find(r);
p[x].relation=(p[x].relation+p[r].relation)%2;//关系的更新
return p[x].pre;//返回根节点
}
int main(){
int t;
scanf("%d",&t);
for(int cas=1;cas<=t;cas++)
{
scanf("%d%d",&n,&m);
int a,b;
for(int i=0;i<=n;i++)
{
p[i].pre=i;
p[i].relation=0;
}
int flag=0;
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
if(flag)continue;
int roota=find(a);
int rootb=find(b);
if(roota!=rootb)
{
//roota->rootb=roota->a+a->b+b->rootb
p[rootb].pre=roota;
p[rootb].relation=(p[a].relation+1-p[b].relation+2)%2;
}
else
{
//a->b=a->roota+roota->rootb+rootb->b
if(1!=(2-p[a].relation+p[rootb].relation+p[b].relation)%2)
flag=1;
}
}
printf("Scenario #%d:\n",cas);
if(flag)
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
printf("\n");
}
return 0;
}