标签:des style blog http java color
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24915 Accepted Submission(s): 7641
题目设置两个情况,1、树只有一个根 2、无环
当 输入 0 0时也输出yes。
代码很容易就写出了:
1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #include <iostream> 5 #include <vector> 6 #include <map> 7 #include <queue> 8 #include <set> 9 #include <vector> 10 #include <string> 11 #include <sstream> 12 using namespace std; 13 14 int father[100005]; 15 int a[100005]; 16 int MAXH, MINH; 17 18 int findroot(int p) 19 { 20 int r, x, j; 21 r=p; 22 while(r!=father[r]) 23 r=father[r]; 24 x=p; 25 while(x!=r) 26 { 27 j=father[x]; 28 father[x]=r; 29 x=j; 30 } 31 return r; 32 } 33 34 main() 35 { 36 int x, y, i, j, k, num, flag=1; 37 for(i=1;i<=100005;i++) father[i]=i; 38 num=0; 39 set<int>se; 40 while(scanf("%d %d",&x,&y)==2&&x!=-1) 41 { 42 if(x==0&&y==0) 43 { 44 if(!flag) 45 { 46 printf("No\n"); 47 for(i=1;i<=100005;i++) father[i]=i; 48 se.clear(); 49 flag=1;continue; 50 } 51 set<int>::iterator it; 52 for(it=se.begin();it!=se.end();it++) 53 if((*it)==findroot(*it)) 54 num++; 55 se.clear(); 56 if(num>1) { 57 printf("No\n"); 58 } 59 else printf("Yes\n"); 60 num=0; 61 for(i=1;i<=100005;i++) father[i]=i; 62 continue; 63 } 64 int fx=findroot(x); 65 int fy=findroot(y); 66 se.insert(x); 67 se.insert(y); 68 if(fx!=fy) 69 { 70 if(fx>fy) swap(fx,fy); 71 father[fy]=fx; 72 } 73 else flag=0; 74 } 75 }
标签:des style blog http java color
原文地址:http://www.cnblogs.com/qq1012662902/p/3840324.html