Time limit:1000 ms
Memory limit:32768kb
64bit Io format:% I64d & % i64usubmit status practice HDU 1272
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. But she had different ideas for designing the maze. First, she thought that all the channels should be two-way connections, that is, if one channel connects 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
Yes yes no data structure basic question: Use and check the set to check whether there are more than two paths between two points. Note that there may be "Two Sets". therefore, I used a vis [], ANS, and determine whether Father [I] = I was used to calculate the number of the Set (ANS ).
1 # include <cstdio> 2 # include <string. h> 3 using namespace STD; 4 // int rank [100010]; 5 Int father [100010]; 6 int max1 = 0; 7 int vis [100010]; 8 int find (int x) 9 {10 if (X! = Father [x]) 11 father [x] = find (father [x]); 12 Return father [X]; 13} 14 bool Union (int x, int y) 15 {16 int X1 = find (x), Y1 = find (y); 17 if (x1! = Y1) 18 {19 father [Y1] = x1; 20 return true; 21 // rank [x] + = rank [y]; 22 // If (max1 <rank [x]) max1 = rank [X]; 23 // return; 24} 25 return false; 26} 27 int main () 28 {29 int A, B; 30 While (scanf ("% d", & A, & B )! = EOF &! =-1 & B! =-1) 31 {32 for (INT I = 1; I <= 100010; I ++) 33 father [I] = I; 34 if (a = 0 & B = 0) {printf ("Yes \ n"); continue;} // pay attention to continue !!! 35 int min = 100000000; 36 int max = 0; 37 int flag = 1; 38 memset (VIS, 0, sizeof (VIS); 39 while (A | B) 40 {41 if (a> MAX) max = A; 42 if (B> MAX) max = B; 43 if (a <min) min =; 44 If (B <min) min = B; 45 vis [a] = 1; 46 vis [B] = 1; 47 If (! Union (a, B) Flag = 0; 48 scanf ("% d", & A, & B); 49} 50 if (flag = 0) printf ("NO \ n"); 51 else {52 int ans = 0; 53 for (INT I = min; I <= max; I ++) 54 if (vis [I] & father [I] = I) 55 ans ++; 56 If (ANS = 1) printf ("Yes \ n "); 57 else printf ("NO \ n"); 58} 59 60} 61 Return 0; 62}
I changed my mind
1 # include <cstdio> 2 # include <string. h> 3 using namespace STD; 4 // int rank [100010]; 5 Int father [100010]; 6 int rank [100010]; 7 int max1 = 0; 8 // int vis [100010]; 9 int find (int x) 10 {11 if (X! = Father [x]) 12 Father [x] = find (father [x]); 13 return father [X]; 14} 15 bool Union (int x, int y) 16 {17 int X1 = find (x), Y1 = find (y); 18 if (x1! = Y1) 19 {20 father [Y1] = x1; 21 22 rank [X1] + = rank [Y1]; // record depth 23 if (max1 <rank [X1]) max1 = rank [X1]; 24 return true; 25 // rank [x] + = rank [y]; 26 // If (max1 <rank [x]) max1 = rank [X]; 27 // return; 28} 29 return false; 30} 31 int main () 32 {33 int A, B; 34 while (scanf ("% d", & A, & B )! = EOF &! =-1 & B! =-1) 35 {36 for (INT I = 1; I <= 100010; I ++) 37 {38 father [I] = I; rank [I] = 1; 39} 40 41 if (a = 0 & B = 0) {printf ("Yes \ n"); continue;} 42 int flag = 1; 43 // memset (VIS, 0, sizeof (VIS); 44 int ans = 0; 45 while (A | B) 46 {47 ans = ans + 1; // This is the number of input edges 48 if (! Union (a, B) Flag = 0; 49 scanf ("% d", & A, & B ); 50} 51 // printf ("% d \ n", ANS, max1); 52 If (flag = 0) printf ("NO \ n "); 53 else if (max1-1 = ans) printf ("Yes \ n"); 54 else printf ("NO \ n"); 55} 56 return 0; 57}