Problem Description:
problem Descriptionthe 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.
Inputinput 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.
Outputfor 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 Input6 8 5 3 5 2 6 for 6 0 08 1 7 3 6 2 8 9 7 4 7 8 7 6 0 03 8 6 8 6, 3 5 6 5 2 0 0-1-1
Sample OutputYesYesNoCode ideas: And check the set, determine whether the final compliance with the conditions have two points: 1, two rooms to have only one road (so can not form a circuit). 2, all the rooms are connected (so only one root node can end up). In the code I use flag in two judgments, if the flag is finally 1, the representative does not meet the criteria, and vice versa.Code:
#include <stdio.h>#include<string.h>intFlag;intpre[2000000];intFindintx) { intR=x; while(r!=Pre[r]) r=Pre[r]; intI=x,j; while(i!=R) {J=Pre[i]; Pre[i]=S; I=J; } returnR;}voidMixintXinty) { intfx=find (x); intfy=find (y); if(fy==FX) Flag=1; PRE[FY]=FX;}intMain () {intN,m,count; while(SCANF ("%d%d", &n,&m)! =EOF) { if(n==-1&&m==-1) Break; Flag=0; Count=0; memset (PRE,-1,sizeof(pre)); while(n| |m) {if(pre[n]==-1) Pre[n]=N; if(pre[m]==-1) Pre[m]=m; Mix (n,m); scanf ("%d%d",&n,&m); } for(intI=0;i<100007; i++) if(pre[i]==i) Count++; if(count>1) Flag=1; if(flag) printf ("no\n"); Elseprintf"yes\n"); } return 0;}
View Code
HDU 1272 The maze of Little Nozomi (and find the second)