Xiaoxi's maze
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 19647 Accepted Submission (s): 6009
Problem Description
The last time Gardon's Labyrinth Castle was played for a long time (see Problem B), she now wants to design a maze for Gardon to come. However, she had different ideas for designing the maze. First, she thought that all the channels should be connected in two directions. That is to say, if one channel connects room A and B, it can be used to go from room A to Room B, or from Room B to room A to improve the difficulty, john hopes that any two rooms can have only one path to communicate with each other (unless they go back ). Xiaoxi now gives you her design drawing to help you determine whether her design drawing conforms to her design idea. For example, in the following example, the first two conditions are met, but the last one has two methods from 5 to 8.
Input
The input contains multiple groups of data. Each group of data is a list of integer pairs ending with 0, indicating the numbers of the two rooms connected by one channel. The number of the room must be at least 1 and cannot exceed 100000. There is an empty row between each two groups of data.
The entire file ends with two-1 characters.
Output
For each group of input data, the output only contains one row. If the maze conforms to Xiao Xi's idea, "Yes" is output; otherwise, "No" is output ". Sample Input
6 8 5 3 5 2 6 45 6 0 08 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 03 8 6 8 6 45 3 5 6 5 2 0 0-1 -1
Sample Output
YesYesNo
Code:
At the beginning, the array is allocated 100 k, which is small... no problem ~
# Include "stdio. h "# include" string. h "int set [100100]; int vis [100100]; int flag; int search (int n) {int I = n; while (set [I]! = I) // find the root node of n, and n must be paid to I because the following statement uses n and cannot change it. I = set [I]; int j = n; while (j! = I) // when n does not point to the root node just found, point all n's fathers to I {// optimize int temp = set [j]; set [j] = I; j = temp;} return I;} void update (int a, int B) {if (search (a) = search (B )) // there is no common root node, indicating that the connection flag is not 1; else set [search (a)] = search (B);} void connect () // determine whether to connect {int I, count = 0; for (I = 1; I <100100; I ++) {if (! Vis [I] & set [I] = I) {count ++; // printf ("count: % d \ n", count );} if (count> 1) {flag = 1; break;} // if the root node number is greater than 1, // as the graph is required, if you point to your own one, there is a Ring} int main () {int a, B, I; while (scanf ("% d", &, & B),! =-1 & B! =-1) {memset (vis, true, sizeof (vis); if (a = 0 & B = 0) {printf ("Yes \ n "); continue;} for (I = 0; I <100100; I ++) // initialize the parent node set [I] = I; flag = 0; update (a, B ); vis [a] = vis [B] = false; // The vertex of the input edge is set to while (scanf ("% d", &, & B), a & B) {update (a, B); vis [a] = vis [B] = false;} connect (); if (flag = 0) printf ("Yes \ n"); else printf ("No \ n ");}}