Little hope labyrinth ring disjoint-set determines whether there

Problem Description
Last Gardon maze castle little hope to play for a long time (see Problem B), and now she wanted to design a maze Gardon to let go. But she maze design ideas are not the same, she thought first of all of the channels should be two-way communication, that is if there is a passage room A and B, then both went to the room from the room A through B it can also be it went through the room a from the room B, to raise the difficulty, any small hope hope the two rooms has one and only one path can communicate (unless go back now). Little hope now her design to you, let you help her determine whether the design of her design ideas. For example the following example, the first two are eligible, but there are two ways to the final arrival 8 from 5. 
 

 

Input
Input data comprises a plurality of sets, each set of data is an integer of 00 to the end of the list, indicates the number of two rooms a channel connection. No room is at least 1 and not more than 100,000. A blank line between each two sets of data. 
Two end in the entire file 1.
 

 

Output
For each set of data inputs, the output comprises only one row. If the idea of ​​the labyrinth in line with little hope, then the output "Yes", otherwise a "No".
 

 

Sample Input
6 8 5 3 5 2 6 4
5 6 0 0

8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0

3 8 6 8 6 4
5 3 5 6 5 2 0 0

-1 -1
 

 

Sample Output
Yes
Yes
No
**************************************************************************************************************************
Disjoint-set
**************************************************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<cstdio>
 6 using namespace std;
 7 int fa[100001],i,j,a,b;
 8 int vis[100001];
 9 int maxn,minn,cnt;
10 int find(int x)
11 {
12     while(x!=fa[x])
13      x=fa[x];
14     return x;
15 }
16 void Unon(int x,int y)
17 {
18     int a=find(x);
19     int b=find(y);
20     if(a!=b)
21      fa[a]=b;
22 }
23 int main()
24 {
25     for(i=1;i<=100000;i++)
26     fa[i]=i;
27     int gs=1;
28     maxn=-1;
29     minn=9999999;
30    while(scanf("%d %d",&a,&b)!=EOF&&(a!=-1&&b!=-1))
31    {
32        bool flag=true; int ans=0;
33        if(a==0&&b==0)
34        {
35           puts("Yes");
36           continue;
37        }
38        int maxn=-1;
39        int minn=99999999;
40        for(i=1;i<100000;i++)
41        {
42            fa[i]=i;
43            vis[i]=0;
44        }
45        while(a||b)
46        {
47            if(maxn<a)maxn=a; if(maxn<b)  maxn=b;
48            if(minn>a)minn=a; if(minn>b)  minn=b;
49            if(find(a)!=Find (B))
 50               UNON (A, B);
 51 is              the else 
52 is                 In Flag = to false ;
 53 is             VIS [A] = VIS [B] = . 1 ;
 54 is             Scanf ( " % D% D " , & A, & B);
 55         }
 56 is         IF (In Flag!) // there circuit 
57 is           the puts ( " No " );
 58         the else 
59         {
 60             for (I = Minn; I <= MAXN; I ++) // If the presence of a plurality of communication block 
61             {
 62                int x=find(i);
63                if(x==i&&vis[i])ans++;
64            }
65            if(ans==1)
66             puts("Yes");
67            else
68             puts("No");
69        }
70        //puts("\n");
71    }
72    return 0;
73 }
View Code

 

Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3399611.html

Guess you like

Origin blog.csdn.net/weixin_33939380/article/details/93432856