要注意有特殊情况0 0,只要记录点是否都记录了,连接的边和用到的点的差是1就可以了。
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 using namespace std; 3 int lu[100001]; 4 int used[100001]; 5 int find(int x) 6 { 7 while(x!=lu[x]) 8 x=lu[x]; 9 return x; 10 } 11 12 int memger(int x,int y) 13 { 14 x=find(x); 15 y=find(y); 16 if(x==y) 17 return 0; 18 lu[x]=y; 19 return 1; 20 } 21 22 int main() 23 { 24 int a,b; 25 int i,j,flag,t; 26 while(cin>>a>>b) 27 { 28 if(a==-1&&b==-1) 29 return 0; 30 if(a==0&&b==0) 31 { 32 cout<<"Yes"<<endl; 33 continue; 34 } 35 for(i=1;i<100001;i++) 36 { 37 used[i]=0; 38 lu[i]=i; 39 } 40 memger(a,b); 41 used[a]=1; 42 used[b]=1; 43 flag=1; 44 t=1; 45 while(cin>>a>>b) 46 { 47 if(a==0&&b==0) 48 break; 49 if(used[a]==0) 50 t++,used[a]=1; 51 if(used[b]==0) 52 t++,used[b]=1; 53 if(memger(a,b)==0) 54 flag=0; 55 else 56 t--; 57 } 58 if(flag && t==1) 59 cout<<"Yes"<<endl; 60 else 61 cout<<"No"<<endl; 62 } 63 return 0; 64 }