"and look Up" hdu 1272 little Greek MazeAnd check the collection--and check the forest without ring and connected
Main TopicGive you a connection relationship. Constructs a graph that determines whether the graph is non-cyclic and satisfies connectivity.
talk about the idea.① has no ring: (A, B) the element A, the root node is the same, the same merged edge will become a ring, do not meet the topic requirements ② Connectivity: Record the node before the last node = = The number of its own node, just 1 is a connected forest!
★ Again later and check set initialization i < _max, this problem has the equal sign on WA, do not know why?!!
Reference Code
//#pragma comment (linker, "/stack:1024000000,1024000000")#include <bits/stdc++.h>#define CLR (k,v) memset (k,v,sizeof (k) )using namespace STD;Const int_max =1e5+Ten;intA,b,pre[_max];BOOLOk,v[_max];voidInit () { for(inti =1; i < _max; + + i) pre[i] = i;}intFindintx) {//check, the root node of element x returnPRE[X] = = x? X:PRE[X] = find (Pre[x]);}voidJoinintAintb) {//And, Edge (A, B) intx = Find (a), y = find (b);if(x! = y) pre[x] = y;ElseOK =false;//Plus (A, b) a loop appears}intMain () {#ifndef Online_judgeFreopen ("Input.txt","R", stdin);#endif //Online_judge while(scanf("%d%d", &a,&b) = =2){if(A = =-1&& b = =-1) Break; Init (); OK =true; CLR (V,0); while(A! =0&& B! =0) {Join (A, b); V[a] = v[b] =1;scanf("%d%d", &a,&b); }intCNT =0; for(inti =1; i < _max; + + i)if(V[i] = =1&&pre[i] = = i) cnt + +;if(CNT >1) OK =false;//Only one connected branch puts(OK?)"Yes":"No"); }return 0;}
- Bold
Ctrl + B
- Italic Body
Ctrl + I
- Reference
Ctrl + Q
- Insert Link
Ctrl + L
- Inserting code
Ctrl + K
- Insert Picture
Ctrl + G
- Promote title
Ctrl + H
- Ordered list
Ctrl + O
- Unordered list
Ctrl + U
- Line
Ctrl + R
- Revoke
Ctrl + Z
- Redo
Ctrl + Y
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
And find out the maze of HDU 1272 little Nozomi