您的位置:首页 > Web前端

poj 2492 A Bug's Life 并查集

2017-04-12 17:43 387 查看
裸的并查集咯

ACcode:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 100010
int myset[maxn];
int find_x(int x){
if(myset[x]==-1)return x;
return myset[x]=find_x(myset[x]);
}
int main(){
int loop,tot=1;
scanf("%d",&loop);
while(loop--){
if(tot>1)printf("\12");
int n,m;
scanf("%d%d",&n,&m);
memset(myset,-1,sizeof(myset));
int flag=0;
while(m--){
int a,b;
scanf("%d%d",&a,&b);
if(find_x(a)!=find_x(b)&&find_x(a)!=find_x(b+n)){
myset[find_x(a)]=find_x(b+n);
myset[find_x(b)]=find_x(a+n);
}
else if(find_x(a)==find_x(b))flag=1;
}
printf("Scenario #%d:\n%s bugs found!\n",tot++,flag==0?"No suspicious":"Suspicious");
}
return 0;
}
/*

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: