Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1272
Note:
1. cannot be a ring, that is, the two numbers of root nodes entered each time cannot be the same;
2. There is only one maze, that is, the number of root nodes is unique;
3. Note that "yes" must be output when only "0 0" is input ";
4. Recursive stack overflow is used for State compression.
Reference code:
#include<stdio.h>int fa[100001];int find(int u){ int s; s=u; while (fa[u]!=u)u=fa[u]; while (fa[s]!=s){fa[s]=u;s=fa[s];} return u;}int main(){ int u,v,x,y,n,m,i,k; bool bo; scanf("%d%d",&u,&v); while (u!=-1) { if (u!=0) { for (i=1;i<=100000;i++) fa[i]=i; bo=true; while (u) { x=find(u);y=find(v); if (x==y) {bo=false;} else fa[x]=y; scanf("%d%d",&u,&v); } if (!bo) printf("No\n"); int j; if(bo) { for (i=1;i<=100000;i++) if (find(i)!=i) break; for (j=i+1;j<=100000;j++) if ((find(j)!=j)&&(find(j)!=find(i))) { printf("No\n"); bo=false; break; } } if (bo) printf("Yes\n"); } else printf("Yes\n"); scanf("%d%d",&u,&v); } return 0;}<strong></strong>
Please refer to: http://blog.csdn.net/yzj577/article/category/2432227