Hdu 1272 Xiao Xi's maze (simple and query set)
Xiaoxi's mazeTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 31396 Accepted Submission (s): 9726
Problem Description the last time Gardon's Labyrinth Castle was played for a long time (see Problem B). Now she wants to design a maze for Gardon. 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.
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.
For each group of input data, 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
Author Gardon
Source HDU 2006-4 Programming Contest
I just learned and checked the set. When I saw this question, I knew that I could use and query the set, but I still couldn't find myself alone .... Looking at other people's ideas: 1. judging whether the ring is closed. 2. As long as the number of nodes = the number of sides + 1. "Yes" can be output only when both conditions are met ". According to this idea, when we first thought about finding the root, if we found that the two roots were the same, we would directly output no and then break it. Otherwise, the relationship between the number of nodes and the edge is determined at the end.
Then Wa .............. Look at others' blogs and find that you need to consider multiple trees. Looking at your own code, I found that I directly broke the AK when determining the root, without considering multiple trees. Once this is done, I will output multiple conclusions.
Code:
# Include
# Include
# Define M 1000005
Int father [M], vis [M];
Int findroot (int x)
{
Int r = x;
While (father [r]! = R)
R = father [r];
Return r;
}
Int merge (int x, int y)
{
Int fx, fy;
Fx = findroot (x); fy = findroot (y );
If (fx! = Fy)
{
If (fx> fy) father [fx] = fy;
Else father [fy] = fx;
}
}
Int main ()
{
Int I, j, k, a, B, mini = M, maxi =-1, t = 1, edge = 0, flag;
While (scanf ("% d", & a, & B )! = EOF)
{
T = 1; edge = 0; flag = 0;
If (a =-1 & B =-1) break;
If (a = 0 & B = 0 ){
Printf ("Yes \ n ");
}
Memset (vis, 0, sizeof (vis ));
For (I = 1; I <= 1000000; I ++)
Father [I] = I;
If (! Vis [a]) vis [a] ++;
If (! Vis [B]) vis [B] ++;
While (scanf ("% d", & a, & B ))
{
If (a = 0 & B = 0)
{
For (I = 1; I <= 1000000; I ++)
If (vis [I]) edge ++; // view the number of nodes
If (t! = Edge-1)
Flag = 1;
If (flag = 0) printf ("Yes \ n ");
Else printf ("No \ n ");
Break;
}
T ++; // Number of viewing Edges
If (! Vis [a]) vis [a] ++;
If (! Vis [B]) vis [B] ++;
If (father [a]! = Father [B])
{
Merge (a, B );
}
Else {
Flag = 1; // There is a break here. If it is found that it is not correct, it is changed to a flag, and the situation of multiple trees is considered.
}
}
}
Return 0;
}