Topic Connection
http://acm.hdu.edu.cn/showproblem.php?pid=1272
The maze of Xiaoxi 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 4
5 6 0 0
8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0
3 8 6 8 6 4
5 3 5 6 5 2 0 0
-1-1
Sample Output
Yes
Yes
No
Hangzhou Electric 100th Mark O (∩_∩) o~
1#include <algorithm>2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <vector>7#include <map>8#include <Set>9 usingstd::cin;Ten usingstd::cout; One usingStd::endl; A usingStd::find; - usingStd::sort; - usingSTD::Set; the usingStd::map; - usingstd::p air; - usingstd::vector; - usingStd::multiset; + usingStd::multimap; - #definePB (E) push_back (e) + #defineSZ (c) (int) (c). Size () A #defineMP (A, b) Make_pair (A, B) at #defineAll (c) (c). Begin (), (c). End () - #defineITER (c) Decltype ((c). Begin ()) - #defineCLS (arr,val) memset (arr,val,sizeof (arr)) - #defineCpresent (c, E) (Find (All (c), (e))! = (c). End ()) - #defineRep (i, n) for (int i = 0; i < (int) (n); i++) - #defineTR (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); ++i) in Const intN =100010; -typedef unsignedLong Longull; to structUnionfind { + BOOLVis[n]; - intPar[n], rank[n]; theInlinevoidInitintN) { * Rep (i, N) { $Par[i] =i;Panax NotoginsengVis[i] =false, rank[i] =0; - } the } +InlineintFindintx) { A while(X! =Par[x]) { thex = par[x] =Par[par[x]]; + } - returnx; $ } $InlineBOOLUniteintXinty) { -x = Find (x), y =find (y); - if(x = = y)return false; the if(Rank[x] <Rank[y]) { -PAR[X] =y;Wuyi}Else { thePar[y] =x; - if(Rank[x] = = Rank[y]) rank[x]++; Wu } - return true; About } $InlinevoidWorkintAintBBOOL&f) { -Vis[a] = vis[b] =true; - if(!unite (A, b)) F =false; - } AInlinevoidjudge () { + intCNT =0; the Rep (i, N) { - //the topic given is not necessarily only a set, it may give a number of sets, each other is not connected $ if(Vis[i] && par[i] = = i) cnt++; the } thePuts1= = cnt?"Yes":"No"); the } the }go; - intMain () { in #ifdef LOCAL theFreopen ("In.txt","R", stdin); theFreopen ("OUT.txt","w+", stdout); About #endif the intA, B; the while(~SCANF ("%d%d", &a, &b) && A + b! =-2) { the if(!a &&!b) {printf ("yes\n");Continue; } + BOOLf =true; - Go.init (N); the while(A | |b) {Bayi Go.work (A, B, f); thescanf"%d%d", &a, &b); the } - if(!f) {Puts ("No");Continue; } - Go.judge (); the } the return 0; the}
View Code
HDU 1272 The maze of little Nozomi