标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 42456 Accepted Submission(s): 13078
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN=100005; int par[MAXN],vis[MAXN]; void prep() { for(int i=0;i<MAXN;i++) { vis[i]=0; par[i]=i; } } int fnd(int x) { if(par[x]==x) { return x; } return par[x]=fnd(par[x]); } void unite(int x,int y) { int a=fnd(x); int b=fnd(y); par[a]=b; } bool same(int x,int y) { return fnd(x)==fnd(y); } int main() { int u,v; while(scanf("%d%d",&u,&v)!=EOF) { if(u==-1&&v==-1) break; prep(); int mx=0; if(u==0&&v==0) { printf("Yes\n"); continue; } bool mark=true; do{ mx=max(max(u,v),mx); vis[u]=1; vis[v]=1; if(!same(u,v)) { unite(u,v); } else { mark=false; } scanf("%d%d",&u,&v); }while(u!=0||v!=0); if(mark) { int root=-1; int cnt=0; for(int i=1;i<=mx;i++) { if(vis[i]) { int fa=fnd(i); if(fa!=root) { root=fa; cnt++; } } } if(cnt==1) { printf("Yes\n"); } else { printf("No\n"); } } else { printf("No\n"); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/program-ccc/p/5685824.html