The maze of Xiaoxi
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 34773 Accepted Submission (s): 10629
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 INPUT6 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 Outputyesyesno
Authorgardon TEST instructions: RT analysis: Give the edge of the relationship, can you get a tree?
Determine whether a tree is a few points (turn):
(1) To determine whether there is a circuit, the loop is not a tree. This can be done with a check set. Once a given two nodes, A and B are part of the same set, then a loop can be determined.
(2) See if there is only one connected branch, if there are multiple, then it becomes the forest. This can be judged using a set of checks. Finally, the collection has only one root node.
(3) Determine whether the vertex number is equal to the number of sides plus 1, if not equal, the description is not a tree.
(4) Determine whether the penetration of the node is <=1, if greater than 1, the description is not a tree;
(5) Determine whether the parent nodes of the two nodes are equal, and if they are equal, they cannot form a tree;
This problem is going to explode?
#pragmaComment (linker, "/stack:1024000000,1024000000")#include<cstdio>#include<string>#include<iostream>#include<cstring>#include<cmath>#include<stack>#include<queue>#include<vector>#include<map>#include<stdlib.h>#include<algorithm>#defineLL __int64using namespacestd;Const intmaxn=100000+5;intP[MAXN];intVIS[MAXN];BOOLFlag;intFINDFA (intx) { returnp[x]==x?x:p[x]=Findfa (p[x]);}voidUniintUintv) { intx=FINDFA (U); inty=Findfa (v); if(x!=y) p[x]=y; Elseflag=true;//If the node is already in the tree, there is a ring .}intMain () {//freopen ("In.txt", "R", stdin); intu,v; while(SCANF ("%d%d", &u,&v) && u!=-1&& v!=-1) { if(u==0&& v==0) {printf ("yes\n"); Continue; } //is a special sentence an empty tree? for(intI=1; i<=maxn;i++) {P[i]=i; Vis[i]=0; } Flag=false; Uni (U,V); Vis[u]=vis[v]=1; while(SCANF ("%d%d", &u,&v) && (u| |v)) {uni (u,v); Vis[u]=vis[v]=1; } intans=0; for(intI=1; i<=maxn;i++) if(Vis[i] && p[i]==i) ans++;//Judging is not the forest if(ans>1) flag=true; if(flag) printf ("no\n"); Elseprintf"yes\n"); } return 0;}
View Code
HDU 1272 The maze of little Nozomi