http://acm.nyist.net/JudgeOnline/problem.php?pid=129
2011-11-03 11:26
429 查看
#include<iostream> #include<cstdio> #define N 10005 using namespace std; int parent ;int r ; void make() { for(int i=0;i<N;i++) { parent[i]=i; r[i]=0; } } int find_set(int x) { return parent[x]==x?x:find_set(parent[x]); } int main() { int m,n,x,y,flag,sum,Case=0; while(1) { make(); flag=1;sum=0; while(~scanf("%d%d",&n,&m)!=EOF) { if(m==-1&&n==-1) return 0; if(n==0&&m==0) { if(flag) { for(int i=1;i<N;i++)判断该树,是不是连通的,,, { if(!r[i]) continue; if(parent[i]==i) sum++; } if(sum>1) flag=0; } if(flag) printf("Case %d is a tree.\n",++Case) ; else printf("Case %d is not a tree.\n",++Case) ; break; } x=find_set(n); y=find_set(m); r =1; r[m]=1; if(x==y) flag=0; else { if(parent[m]==m) parent[m]=x;防止一个点有两个根结点。。。 else flag=0; } } }return 0; }
相关文章推荐
- http://acm.nyist.net/JudgeOnline/problem.php?pid=510
- 求余数 http://acm.nyist.net/JudgeOnline/problem.php?pid=205
- nyoj_532_http://acm.nyist.net/JudgeOnline/problem.php?pid=532
- 街区最短路径问题 http://acm.nyist.net/JudgeOnline/problem.php?pid=7
- http://acm.nyist.net/JudgeOnline/problem.php?pid=290
- http://acm.nyist.net/JudgeOnline/problem.php?pid=20
- http://acm.nyist.net/JudgeOnline/problem.php?pid=239
- http://acm.nyist.net/JudgeOnline/problem.php?pid=520
- http://acm.nyist.net/JudgeOnline/problem.php?pid=128&前缀式计算
- http://acm.nyist.net/JudgeOnline/problem.php?pid=488
- http://acm.nyist.net/JudgeOnline/problem.php?pid=119&&Rmq
- http://acm.nyist.net/JudgeOnline/problem.php?pid=517
- 中缀式变后缀式&&http://acm.nyist.net/JudgeOnline/problem.php?pid=467
- 会场安排问题 http://acm.nyist.net/JudgeOnline/problem.php?pid=14
- http://acm.nyist.net/JudgeOnline/problem.php?pid=13
- 字母统计 http://acm.nyist.net/JudgeOnline/problem.php?pid=241
- http://acm.nyist.net/JudgeOnline/problemrank.php?pid=525
- 数数http://acm.nyist.net/JudgeOnline/problem.php?pid=198
- hdu1114,poj1384(完全背包)另附:http://acm.nyist.net/JudgeOnline/problem.php?pid=311
- 喷水装置(一)http://acm.nyist.net/JudgeOnline/problem.php?pid=6