小希的迷宫
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 44168 Accepted Submission(s): 13659
![](https://i-blog.csdnimg.cn/blog_migrate/f8d0703ec2754582a2ff12a31d004e0a.png)
整个文件以两个-1结尾。
6 8 5 3 5 2 6 4 5 6 0 0 8 1 7 3 6 2 8 9 7 5 7 4 7 8 7 6 0 0 3 8 6 8 6 4 5 3 5 6 5 2 0 0 -1 -1
Yes YesNo
#include<stdio.h> #include<string.h> #define N 100001 int father[N],mark[N]; int find(int x){ if(x!=father[x]) father[x]=find(father[x]); return father[x]; } int merge(int a,int b){ int f1=find(a); int f2=find(b); if(f1==f2) return 0; father[f1]=f2; return 1; } int main(){ int x,y; while(scanf("%d%d",&x,&y)!=EOF){ if(!x&&!y) { puts("Yes"); continue; } if(x==-1&&y==-1) break; for(int i=1;i<=N;i++) father[i]=i; memset(mark,0,sizeof(mark)); mark[x]=mark[y]=1; merge(x,y); int n=1,flag=1; while(scanf("%d%d",&x,&y)!=EOF){ if(!x&&!y) break; if(!mark[x]){ n++; mark[x]=1; } if(!mark[y]){ n++; mark[y]=1; } if(merge(x,y)) n--; else flag=0; } if(flag==1&&n==1) puts("Yes"); else puts("No"); } return 0; }