POJ 2492 A Bug's Life
这题跟 POJ 1703 Find them and Catch them 是一样一样的,就不讲解了,不懂可以看
http://www.cnblogs.com/whatbeg/p/3498416.html
代码:
#include <iostream> #include <cstdio> using namespace std; #define N 2100 int fa[N],sa[N]; void makeset(int n) { for(int i=1;i<=n;i++) { fa[i] = i; sa[i] = 0; // 0 : same 1 : different } } int findset(int x) { if(x != fa[x]) { int tmp = fa[x]; fa[x] = findset(fa[x]); sa[x] = (sa[tmp] + sa[x]) % 2; } return fa[x]; } void unionset(int a,int b) { int x = findset(a); int y = findset(b); if(x == y) return; fa[x] = y; sa[x] = (sa[a] + sa[b] + 1)%2; return; } int main() { int t,n,m,i; int a,b; int cs = 1; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); makeset(n); int flag = 1; for(i=1;i<=m;i++) { scanf("%d%d",&a,&b); if(findset(a) == findset(b)) { if(sa[a] == sa[b]) { flag = 0; } } else unionset(a,b); } printf("Scenario #%d:\n",cs++); if(flag) cout<<"No suspicious bugs found!"<<endl; else cout<<"Suspicious bugs found!"<<endl; cout<<endl; } return 0; }
作者:whatbeg
出处1:http://whatbeg.com/
出处2:http://www.cnblogs.com/whatbeg/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
更多精彩文章抢先看?详见我的独立博客: whatbeg.com