链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1272
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#problem/M
代码:
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<algorithm> #include<queue> #include<iostream> using namespace std; #define N 105000 #define INF 0xfffffff #define max(a,b) (a>b?a:b) int f[N]; void Inn() { int i; for(i=0; i<N; i++) f[i]=i; } int Find(int x) { while(x!=f[x]) x = f[x]; return f[x]; } int main() { int a,b,i,flag[N],aa=0,bb=0, j, ans=0; memset(flag,0,sizeof(flag)); Inn(); while(1) { scanf("%d %d",&a,&b); if(a==-1&&b==-1) break; if(!a&&!b) { if(!aa && !bb) printf("Yes "); else if(ans==1) printf("No "); else if(aa==bb+1) printf("Yes "); else printf("No "); aa=bb=ans=0; memset(flag,0,sizeof(flag)); Inn(); continue; } if(!flag[a]) { flag[a]=1; aa++; } if(!flag[b]) { flag[b]=1; aa++; } int na=Find(a); int nb=Find(b); if(na!=nb) { f[na]=nb; bb++; } else ans=1; } return 0; }