Little Nozomi's Maze
Time limit:2000/1000 MS (java/others) Mem Ory limit:65536/32768 K (java/others)
Problem Description last Gardon Maze Castle Little Nozomi played for a long time (see Problem B), now she also want to design a maze let Gardon to go. But she designed the maze of different ideas, first she thought all the channels should be two-way connectivity, that is, if there is a channel connected to room A and B, then can go through it from room A to room B, but also through it from Room B to room A, in order to improve the difficulty, Xiaoxi hope that any two rooms have and only one path can be connected (unless you go back). Xiao-Nozomi now gives you her design to help you determine whether her design is in line with her design ideas. For example, the first two are eligible, but the last one has two methods of reaching 8 from 5.
Input inputs contain multiple sets of data, each of which is a list of integer pairs ending in 0 0, representing the number of two rooms to which a channel is connected. The number of the room is at least 1 and no more than 100000. There is a blank line between each of the two sets of data.
The entire file ends with two-1.
Output contains only one row for each set of data that is entered. If the maze conforms to Xiaoxi's idea, then output "Yes", otherwise output "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 this and check the topic and other topics, there are several points to note the place: 1. Room number is not 1-n, some numbers do not appear, so you want to use an array to mark whether the number appears 2. If the root node of the two numbers entered is the same, it indicates that they are connected, that there is more than one edge on the same path, and that the flag is used to record if there are multiple paths; 3. To determine whether all rooms belong to the same collection.
Flag[i] Whether the array tag I appears, whether the flag tag has a ring, sum records the number of collections #include <stdio.h> const int N = 100005;
int flag[n], father[n]; void Init () {for (int i = 0; I <= 100000; i++) Flag[i] = 0, father[i] = i;} int Find (int x) {if (x! =
FATHER[X]) father[x] = Find (father[x]);
return father[x];
} void Merge (int a, int b) {int p = Find (a);
int q = Find (b);
if (p > Q) father[p] = q;
else father[q] = p;
} int main () {int A, B;
while (~SCANF ("%d%d", &a,&b)) {if (a = =-1 && b = =-1) break;
Init ();
int FLAG = 0;
while (1) {if (a = = 0 && b = = 0) break;
if (find (a) = = find (b)) FLAG = 1;
Merge (A, b);
Flag[a] = 1, flag[b] = 1;
scanf ("%d%d", &a,&b);
} if (FLAG = = 1) printf ("no\n");
else {int sum = 0; for (int i = 0; I <= 100000;
i++) if (Flag[i] && father[i] = = i) sum++;
if (Sum > 1) printf ("no\n");
else printf ("yes\n");
}} return 0; }