标签:
问题描述:
#include<stdio.h> #include<string.h> int flag; int pre[2000000]; int find(int x) { int r=x; while(r!=pre[r]) r=pre[r]; int i=x,j; while(i!=r) { j=pre[i]; pre[i]=r; i=j; } return r; } void mix(int x,int y) { int fx=find(x); int fy=find(y); if(fy==fx) flag=1; pre[fy]=fx; } int main() { int n,m,count; while(scanf("%d%d",&n,&m)!=EOF) { if(n==-1&&m==-1) break; flag=0; count=0; memset(pre,-1,sizeof(pre)); while(n||m) { if(pre[n]==-1) pre[n]=n; if(pre[m]==-1) pre[m]=m; mix(n,m); scanf("%d%d",&n,&m); } for(int i=0;i<100007;i++) if(pre[i]==i) count++; if(count>1) flag=1; if(flag) printf("No\n"); else printf("Yes\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/burning-flame/p/4930997.html