小希的迷宫---hdu1272

http://acm.hdu.edu.cn/showproblem.php?pid=1272

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<math.h>
 6 #define N 100010
 7 #define INF 0xfffffff
 8 using namespace std;
 9 
10 int f[N],vis[N];
11 
12 int Find(int x)
13 {
14     if(x!=f[x])
15         f[x]=Find(f[x]);
16     return f[x];
17 }
18 
19 int main()
20 {
21     int a, b, pa, pb, flag, i;
22     while(scanf("%d%d",&a,&b),a!=-1||b!=-1)
23     {
24         for(i=0;i<N;i++)
25             f[i]=i;
26         memset(vis,0,sizeof(vis));
27         flag=0;
28         if(a==0&&b==0)
29         {
30             printf("Yes\n");//空树也是树;
31             continue;
32         }
33         vis[a] = vis[b] = 1;//说明此点出现过;
34         pa = Find(a);
35         pb = Find(b);
36         if(pa != pb)
37             f[pb] = pa;
38         else
39             flag = 1;
40         while(scanf("%d%d", &a, &b), a+b)
41         {
42             vis[a] = vis[b] = 1;
43             pa = Find(a);
44             pb = Find(b);
45             if(pa != pb)
46                 f[pb]=pa;
47             else
48                 flag=1;
49         }
50         int m=0;
51         for(i=1; i<N; i++)
52         {
53             if(vis[i]==1 && f[i]==i)//只能有一个父节点是本身
54             {
55                 m++;
56                 if(m>1)
57                 {
58                     flag=1;break;
59                 }
60             }
61         }
62         if(flag==1)
63             printf("No\n");
64         else
65             printf("Yes\n");
66     }
67     return 0;
68 }

 

你可能感兴趣的:(HDU)