标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 40762 Accepted Submission(s): 12552
#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> using namespace std; const int N = 100005; struct Edge{ int s,e; }edge[N]; int father[N]; int Hash[N]; ///因为输入的树的编号不连续,离散化一下 int _find(int x){ if(x==father[x]) return x; return _find(father[x]); } int Union(int a,int b){ int x = _find(a); int y = _find(b); if(x==y) return 0; father[x]=y; return 1; } int main() { int x,y; while(scanf("%d%d",&x,&y)!=EOF){ if(x==-1&&y==-1) break; if(x==0&&y==0) { ///空树也是树 printf("Yes\n"); continue; } for(int i=1;i<N;i++) father[i]=i; memset(Hash,-1,sizeof(Hash)); int k=1,m=1; if(Hash[x]==-1) Hash[x]=k++; if(Hash[y]==-1) Hash[y]=k++; edge[m].s = Hash[x]; edge[m++].e = Hash[y]; while(scanf("%d%d",&x,&y)!=EOF){ if(x==0&&y==0) break; if(Hash[x]==-1) Hash[x]=k++; if(Hash[y]==-1) Hash[y]=k++; edge[m].s = Hash[x]; edge[m++].e = Hash[y]; } bool flag = true; for(int i=1;i<m;i++){ if(Union(edge[i].s,edge[i].e)==0){ flag = false; break; } } int ans = 0; for(int i=1;i<k;i++){ if(father[i]==i) ans++; } if(ans==1&&flag) printf("Yes\n"); else printf("No\n"); } }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5475197.html