Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 47191 Accepted Submission(s): 14667
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<queue> #include<map> #include<set> #include<vector> #include<cstdlib> #include<string> #define eps 0.000000001 typedef long long ll; typedef unsigned long long LL; using namespace std; const int N=100000+100; int parent[N]; int vis[N];/* int find(int x) { if(x != parent[x]) parent[x] = find(parent[x]); return parent[x]; }*/ int find(int x){ int r=x; while(r!=parent[r])r=parent[r]; int i=x; int j; while(parent[i]!=r){ j=parent[i]; parent[i]=r; i=j; } return r; } void Union(int x,int y){ x=find(x); y=find(y); //if(x==y)return; if(x<y)parent[x]=y; else parent[y]=x; } int main(){ int m,n; while(scanf("%d%d",&m,&n)!=EOF){ int flag=1; memset(vis,0,sizeof(vis)); for(int i=1;i<=N;i++)parent[i]=i; if(m==-1&&n==-1)break; while(1){ if(m==0&&n==0)break; if(find(m)==find(n))flag=0; Union(m,n); vis[m]=1; vis[n]=1; cin>>m>>n; } if(flag==0){cout<<"No"<<endl;continue;} int ans=0; for(int i=1;i<=N;i++){ if(vis[i]&&parent[i]==i)ans++; } if(ans>1)cout<<"No"<<endl; else{ cout<<"Yes"<<endl; } } }
原文:http://www.cnblogs.com/Aa1039510121/p/6443114.html