The Maze of Xiaoxi
Time limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64u
Description
The last time 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
input contains multiple sets of data, each set of data is a list of integers ending in 0 0, representing the number of two rooms connected by a channel. 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
for each set of data entered, the output includes only one row. If the maze conforms to Xiaoxi's idea, then output "Yes", otherwise output "No".
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
Analysis can think of IS and check the set, here in the input two nodes to determine whether their boss is the same, if it is, then repeat (two points have been connected to add a link to the two points of the road). Be careful to determine if there are orphaned nodes, and do not write yes, note examining.
1#include <cstdio>2#include <cstring>3 4 intpre[100050];5 //int vis[1050];6 intFlag =0;7 8 intFind (intx)9 {Ten intR=x; One while(r! =Pre[r]) AR =Pre[r]; - - intI=x,j; the while(Pre[i]! =R) - { -j =Pre[i]; -Pre[i] =R; +i =J; - } + returnR; A } at - voidJoinintXinty) - { - intFx=find (x), fy=Find (y); - - if(FX! =fy) inPRE[FY] =FX; - to } + - intMain () the { * intn,m,i,j,s,t; $ Panax Notoginseng while(1) - { the intsum =0; +Flag =0; Amemset (PRE,0,sizeof(pre)); the + while(~SCANF ("%d%d", &m,&n) && n &&m) - { $ if(n==-1&& m==-1)return 0; $ if(m==0&& n==0) Break; - - if(pre[m]==0) pre[m]=m; the if(pre[n]==0) pre[n]=N; - Wuyi if(Find (n) = =Find (m)) theFlag =1; - Wu join (m,n); - About } $ - - for(sum =0, i=1; i<=100000; i++) - if(pre[i]==i) sum++; A //determine if there are orphaned nodes + the if(sum>1|| Flag) printf ("no\n"); - Elseprintf"yes\n"); $ //Note Case the the the } the -}
HDU 1272 Little Nozomi's Maze (and check)