标签:
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 Yes No
#include<cstdio> #include<iostream> #include <cstring> #include<set> using namespace std; #define maxn 1000001 int father[maxn]; void init() { for(int i=0;i<maxn;i++) father[i]=i; } int Find(int x) { return x==father[x]?x:father[x]=Find(father[x]); } void Union(int x,int y) { int fx=Find(x); int fy=Find(y); if(fx!=fy) father[fy]=fx; } int main() { //freopen("in.txt", "r", stdin); int a,b; set<int> s; set<int>::iterator ite;; while(scanf("%d%d",&a,&b)!=EOF&&(a!=-1&&b!=-1)) { int c=1; init(); s.clear(); int flag=1; if(a==0&&b==0) { cout<<"Yes"<<endl; continue; } s.insert(a); s.insert(b); Union(a,b); while(scanf("%d%d",&a,&b)!=EOF) { if(a==0&&b==0) { for(ite=s.begin();ite!=s.end();ite++) if(*ite==father[*ite]) c++; break; } else{ if(Find(a)==Find(b)) flag=0; else Union(a,b); } } if(!flag||c!=1) cout<<"No"<<endl; else cout<<"Yes"<<endl; } return 0; }
标签:
原文地址:http://blog.csdn.net/novak_djokovic/article/details/44810991