标签:des style blog http color os java io strong
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26818 Accepted Submission(s):
8273
1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <cstring> 5 using namespace std; 6 7 int father[100009]; 8 9 void set() 10 { 11 for (int i=1; i<100009; i++) 12 father[i]=i; 13 } 14 15 int find(int a) 16 { 17 while (father[a]!=a) 18 a=father[a]; 19 return a; 20 } 21 22 int Union(int x,int y) 23 { 24 x=find(x); 25 y=find(y); 26 if (x!=y) 27 { 28 father[x]=y; 29 return 0; 30 } 31 else 32 return 1; 33 } 34 35 int main () 36 { 37 int a,b,k,used[100009]; 38 while (~scanf("%d%d",&a,&b)) 39 { 40 if (a==0&&b==0) 41 { 42 printf ("Yes\n"); 43 continue; 44 } 45 memset(used,0,sizeof(used)); 46 queue<int>q; 47 q.push(a); 48 q.push(b); 49 used[a]=1; 50 used[b]=1; 51 52 int flag=0,cmp=0; 53 set(); 54 if (a==-1&&b==-1) 55 break; 56 Union(a,b); 57 while (scanf("%d%d",&a,&b),a||b) 58 { 59 if (Union(a,b)==1) 60 flag=1; 61 if(used[a]!=1) 62 { 63 q.push(a); 64 used[a]=1; 65 } 66 if (used[b]!=1) 67 { 68 q.push(b); 69 used[b]=1; 70 } 71 } 72 while (!q.empty()) 73 { 74 int s=q.front(); 75 q.pop(); 76 if (s==father[s]) 77 cmp++; 78 } 79 if (flag==0&&cmp==1) 80 printf ("Yes\n"); 81 else 82 printf ("No\n"); 83 } 84 return 0; 85 }
标签:des style blog http color os java io strong
原文地址:http://www.cnblogs.com/qq-star/p/3938177.html