标签:
Description
Input
Output
Sample Input
Sample Output
1 # include<iostream> 2 # include<cstdio> 3 # include<algorithm> 4 # include<cstring> 5 # include<set> 6 using namespace std; 7 const int N=1000005; 8 struct edge 9 { 10 int fr,to; 11 }; 12 edge e[N]; 13 int pre[N],n,cnt; 14 int mark[N]; 15 void add(int u,int v) 16 { 17 e[cnt].fr=u; 18 e[cnt].to=v; 19 ++cnt; 20 } 21 int find(int x) 22 { 23 int u=x; 24 while(pre[u]!=-1) 25 u=pre[u]; 26 int v; 27 while(pre[x]!=-1){ 28 v=x; 29 x=pre[x]; 30 pre[v]=u; 31 } 32 return u; 33 } 34 bool ok() 35 { 36 memset(pre,-1,sizeof(pre)); 37 for(int i=0;i<cnt;++i){ 38 int u=find(e[i].fr); 39 int v=find(e[i].to); 40 //cout<<e[i].fr<<‘ ‘<<e[i].to<<‘ ‘<<u<<‘ ‘<<v<<endl; 41 if(u==v) 42 return false; 43 pre[u]=v; 44 } 45 return true; 46 } 47 int main() 48 { 49 int a,b; 50 while(scanf("%d%d",&a,&b)) 51 { 52 if(a==-1&&b==-1) 53 break; 54 if(a==0&&b==0){ 55 printf("Yes\n"); 56 continue; 57 } 58 cnt=0; 59 memset(mark,0,sizeof(mark)); 60 int num=0; 61 mark[a]=mark[b]=1; 62 num+=2; 63 add(a,b); 64 //add(b,a); 65 while(scanf("%d%d",&a,&b),a+b){ 66 if(!mark[a]) 67 ++num; 68 if(!mark[b]) 69 ++num; 70 mark[a]=mark[b]=1; 71 add(a,b); 72 //add(b,a); 73 } 74 //cout<<cnt/2<<‘ ‘<<num-1<<endl; 75 if(cnt==num-1&&ok()) 76 printf("Yes\n"); 77 else 78 printf("No\n"); 79 } 80 return 0; 81 }
标签:
原文地址:http://www.cnblogs.com/20143605--pcx/p/4681920.html