Link : http://acm.hdu.edu.cn/showproblem.php?pid=1272
Problem 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
Thinking of solving problems: Use and check the requirements to add elements to the collection to solve the problem. When judging the loop, the two points are formed as long as the two points of the input side have a common parent node.
Note: only 0 0 is in compliance, so the "Yes" should be output. The code is for informational purposes only (
and look up the set template):
1#include <cstdio>2#include <cstdlib>3#include <iostream>4 Const intMAXN =100005;5 Const intINF =99999999;6 using namespacestd;7 intpar [MAXN];//parent Node8 intSZ[MAXN];///height, size9 intBian, Dian;///edges and pointsTen intFlag =0;//for the resolution of special elements 0 0 One voidinit () { A for(inti =1; i < MAXN; ++i) { -Par[i] =i; -Sz[i] =1; the } - } - intFindintx) { - if(Par[x] = =x) { + returnx; -}Else { + returnfind (Par[x]); A } at } - voidUnite (intXinty) { - intFX = Find (x), FY =find (y); - if(FX = = FY)return;///The family doesn't add it to the collection. - if(Sz[fx] >Sz[fy]) { -PAR[FY] =FX; inSZ[FX] + =Sz[fy]; - if(Dian < SZ[FX])//record the points added to the collection toDian =SZ[FX]; +}Else { -PAR[FX] =fy; theSz[fy] + =SZ[FX]; * if(Dian < sz[fy])//record the points added to the collection $Dian =Sz[fy];Panax Notoginseng } - } the intMain () { + while(1) { ABian =0, Dian =0; the intA, B; + init (); - while(SCANF ("%d%d", &a, &b)! =EOF) { $ if(A = =-1&& b = =-1)//Input-1-1 End program $Exit (0); - if(A = =0&& b = =0){ - if(Flag = =0) {////0 0 is the first time the input dian++ theDian + +; - }Wuyi if(Flag = =1)//Point 0 0 is not the first time it is entered, then flag is returned to 0, in order to meet the next theFlag =0; - Break; Wu } - if(A! =b) { About if(Flag = =0)//when there is not 0 0 input, the flag is assigned a value of 1, for the last input 0 0 when the cushion $Flag =1; -Bian + +; - Unite (A, b); - } A } + if(Dian-1==bian) theprintf"yes\n"); - Else $printf"no\n"); the } the return 0; the}
View Code
Another form of expression (C + +):
1#include <cstdio>2#include <iostream>3#include <map>4#include <cstring>5 using namespacestd;6 intMain ()7 {8 inti,j,k,t;9 int from, to;TenMap <int,int>Mymap; One intCNT =0; A while(cin>> from>>to,~ from|| ~to ) - { -CNT =0;//several sets of data the while(1) - { - if(! ( from||to )) - Break; +cnt++; - if(Mymap.count ( from)==0) +mymap[ from] =to ; A if(Mymap.count (TO) = =0) atMymap[to] = from; -Cin>> from>>to ; - } - if(mymap.size () = =0) - { -Puts"Yes"); in Continue; - } to if(mymap.size () = = (cnt+1)) +Puts"Yes"); - Else thePuts"No"); * mymap.clear (); $ }Panax Notoginseng return 0; -}
View Code
Welcome code friends to review, grow together.