标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 33175 Accepted Submission(s):
10210
1 #include<stdio.h> 2 #include<algorithm> 3 using namespace std; 4 int set[100010]; 5 bool mark[100010]; 6 int flag; 7 int find(int x) 8 { 9 int r=x; 10 while(set[r]!=r) 11 r=set[r]; 12 return r; 13 } 14 void merge(int x,int y) // 15 { 16 int fx,fy; 17 fx=find(x); 18 fy=find(y); 19 if(fx!=fy) 20 set[fx]=fy; 21 } 22 int main() 23 { 24 int a,b,i; 25 while(scanf("%d%d",&a,&b)!=EOF) 26 { 27 flag=0;int cnt=0; 28 if(a==-1&&b==-1)break; 29 if(a==0&&b==0) 30 { 31 printf("Yes\n"); 32 continue; 33 } 34 for(i=0;i<100010;i++) 35 { 36 set[i]=i; 37 mark[i]=0; 38 } 39 int max=-1,min=100010; 40 while(a||b) 41 { 42 if(a>max)max=a; 43 if(b>max)max=b; 44 if(a<min)min=a; 45 if(b<min)min=b; 46 mark[a]=1;mark[b]=1; //标记已经出现出现的元素 ; 47 int aa=find(a); 48 int bb=find(b); 49 if(aa==bb) 50 { 51 flag=1; //关系重复 ;但是1 2 2 1 0 0 应该是Yes ; 52 } 53 else 54 merge(aa,bb); 55 scanf("%d%d",&a,&b); 56 } 57 if(flag==1) //关系重复 ; 不满足题意 ; 58 printf("No\n"); 59 else 60 { 61 for(i=min;i<=max;i++) 62 { 63 if(mark[i]&&set[i]==i) //判读集合个数 ; 64 cnt++; 65 } 66 if(cnt==1) 67 printf("Yes\n"); 68 else 69 printf("No\n"); 70 } 71 } 72 return 0; 73 }
//不能理解;掌握知识太少;我的代码:一直显示爆栈;
1 #include <stdio.h> 2 #define min(a, b) a<b?a:b 3 #define max(a, b) a>b?a:b 4 int father[100010], mark[100010], ans ; 5 int find(int a) 6 { 7 if(father[a] != a) 8 father[a] = find(father[a]) ; 9 return father[a] ; 10 } 11 int mercy(int a, int b) 12 { 13 a = find (a) ; 14 b = find (b) ; 15 if(find(a) != find(b)) 16 father[a] = b ; 17 else 18 ans = 0 ; 19 } 20 int main() 21 { 22 int a, b, i, mini, maxi ; 23 while(~scanf("%d %d", &a, &b), a!=-1) 24 { 25 if(a == 0) 26 { 27 printf("Yes\n") ; 28 continue ; 29 } 30 for(i=0; i<100001; i++) 31 {father[i] = i ; mark[i] = 0 ;} 32 mark[a] = mark[b] = 1 ; 33 mini = min(a, b) ; 34 maxi = max(a,b) ; 35 mercy(a, b) ; 36 ans = 1; 37 while(~scanf("%d %d", &a, &b), a+b) 38 { 39 mark[a] = mark[b] = 1 ; 40 int x, y ; 41 x = min(a, b) ; 42 y = max(a, b) ; 43 if(x < mini) mini = x ; 44 if(y > maxi) maxi = y ; 45 mercy(a, b) ; 46 } 47 48 int as = 0 ; 49 for(i=mini; i<=maxi; i++) 50 { 51 if(mark[i] && father[i] == i) 52 as++ ; 53 if(as > 1) 54 { 55 ans=1 ; break ; 56 } 57 } 58 if(ans) 59 printf("Yes\n") ; 60 else 61 printf("No\n") ; 62 } 63 return 0 ; 64 }
//擦, 一下午+一晚上研究了两个别人错误代码 ; 坑人, 瞬间爆炸 ;
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4663127.html