poj_2492 A Bug's Life
2013-09-28 10:48
357 查看
原题链接:http://poj.org/problem?id=2492
分析:
题意分析:有N个bug,和M次描述,每次描述表示两个编号的bug互相喜欢。。问没有同性关系。
算法分析:多开一个sex数组,存对方的性别。这样同一个性别的bug就可以合并了。
我的代码:
总结:真没从题意中看出每个case之间要空一行。PE了一次。
分析:
题意分析:有N个bug,和M次描述,每次描述表示两个编号的bug互相喜欢。。问没有同性关系。
算法分析:多开一个sex数组,存对方的性别。这样同一个性别的bug就可以合并了。
我的代码:
#include<stdio.h> #include<string.h> #define MAXN 2005 int f[MAXN]; int sex[MAXN]; int n,m; int find(int i){ if(i==f[i]) return i; else return f[i]=find(f[i]); } bool flag; void Union(int a,int b){ a=find(a); b=find(b); if(a!=b){ f[a]=b; } } void Solve(int a,int b){ int t1=find(a); int t2=find(b); if(sex[t1]==0) sex[t1]=t2; else{ if(t1==t2) { flag=true; // printf("OK?"); } else Union(sex[t1],t2); } if(sex[t2]==0) sex[t2]=t1; else{ if(t1==t2) flag=true; else Union(sex[t2],t1); } } int main() { int t; scanf("%d",&t); for(int kase=1;kase<=t;kase++){ flag=false; //没发现有同性。 scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { sex[i]=0; //存对方的性别。 f[i]=i; } while(m--){ int a,b; scanf("%d%d",&a,&b); if(flag) continue; Solve(a,b); } printf("Scenario #%d:\n",kase); if(flag) printf("Suspicious bugs found!\n"); else printf("No suspicious bugs found!\n"); if(kase!=t) printf("\n"); } return 0; }
总结:真没从题意中看出每个case之间要空一行。PE了一次。
相关文章推荐
- POJ 2492 A Bug's Life
- poj2492——A Bug's Life
- POJ 2492 A Bug's Life
- [NWPU][2014][TRN][12]并查集D - A Bug's Life POJ 2492
- poj 2492 A Bug's Life 【并查集拓展】
- POJ A Bug's Life POJ 2492(和食物链差不多的一道题)
- POJ 2492 A Bug's Life
- POJ 2492 A Bug's Life
- poj 2492 A Bug's Life
- 【种类并查集】-POJ-2492-A bug's life
- poj2492 - A Bug's Life
- A Bug's Life poj 2492
- POJ 2492 A Bug's Life 并查集维护类别
- POJ-2492 A Bug's Life
- POJ 2492 A Bug's Life
- POJ 2492 A Bug's Life
- Poj 2492 A Bug's Life
- POJ 2492 A Bug's Life
- POJ - 2492 Bug's Life(种族并查集的简单应用)
- poj2492 A Bug's Life