http://acm.hdu.edu.cn/showproblem.php?pid=3594
Determines whether a given graph is strongly connected, and each edge belongs to only one connected component.
A strong connection is determined by determining whether the vertex number is 1 after the indent.
Then, in the process of shrinking, if a ring has been produced, and the parent node of the present node has a parent node, there must be more than one ring.
Finally, we must judge each node to belong to only one unicom component, otherwise it does not meet the requirements.
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <vector>5#include <cstring>6#include <algorithm>7#include <string>8#include <Set>9#include <functional>Ten#include <numeric> One#include <sstream> A#include <stack> -#include <map> -#include <queue> the - #defineCL (arr, Val) memset (arr, Val, sizeof (arr)) - - #definell Long Long + #defineINF 0x7f7f7f7f - #defineLC L,m,rt<<1 + #defineRC M + 1,r,rt<<1|1 A #definePi ACOs (-1.0) at - #defineL (x) (x) << 1 - #defineR (x) (x) << 1 | 1 - #defineMID (L, R) (L + R) >> 1 - #defineMin (x, y) (x) < (y)? (x): (y) - #defineMax (x, y) (x) < (y)? (y): (x) in #defineE (x) (1 << (x)) - #defineIabs (x) (x) < 0? -(x): (x) to #defineOut (x) printf ("%i64d\n", X) + #defineLowbit (x) (x) & (-X) - #defineRead () freopen ("A.txt", "R", stdin) the #defineWrite () freopen ("Dout.txt", "w", stdout); * $ using namespacestd;Panax Notoginseng #defineN 20100 - //n is the maximum number of points the #defineM 50100 + //m is the maximum number of sides A intN, M;//N M is the number of points and sides the + structedge{ - int from, to, NEX; $ BOOLSign//whether it is a bridge $}edge[m<<1]; - intHead[n], edgenum; - voidAddintUintV) {//the beginning and end of the edge theEdge E={u, V, Head[u],false}; -Edge[edgenum] =E;WuyiHead[u] = edgenum++; the } - //Dnf[i] Indicates that the DFS is the first time to traverse to point I Wu //Low[u] indicates that a subtree with a U point as the parent node can connect to the DFN value of the topmost point in the stack (in other words, the smallest dfn, because the DFN at the top is the smallest) - intDfn[n], Low[n], Stack[n], top, time;//Low[u] is the dfn[v of the point set {U-point and subtree in the root of the U-point (all reverse arcs) that can point to (the nearest ancestor V from the root ) (i.e., V-point timestamp) About intTaj//connecting branch designator, starting from 1 $ intBelong[n];//Belong[i] represents the connected branch where I point belongs - BOOLInstack[n],flag; -vector<int> Bcc[n];//marking starting from 1 - A voidTarjan (intU,intFA) { +Dfn[u] = Low[u] = + +Time ; theStack[top + +] =u; -Instack[u] =1 ; $ the for(inti = Head[u]; ~i; i =Edge[i].nex) { the intv =edge[i].to; the if(Dfn[v] = =-1) the { - Tarjan (V, u); inLow[u] =min (Low[u], low[v]); the if(Dfn[u] <Low[v]) the { AboutEdge[i].sign =1;//to cut the bridge . the } the } the Else if(Instack[v]) + { -Low[u] =min (Low[u], dfn[v]); the if(Dfn[v]!=low[v]) flag=1;//parent node is not yet a root nodeBayi } the } the if(Low[u] = =Dfn[u]) { - intNow ; -Taj + +; Bcc[taj].clear (); the Do{ thenow = stack[--top]; theInstack[now] =0 ; the if(belong[now]!=-1) flag=1;//Each node must belong to only one Unicom component -Belong [Now] =Taj; the Bcc[taj].push_back (now); the} while(Now! =u); the }94 } the the voidTarjan_init (intAll ) { thememset (DFN,-1,sizeof(DFN));98memset (Instack,0,sizeof(Instack)); Aboutmemset (belong,-1,sizeof(Belong)); -top = time = Taj =0;101 for(intI=1; i<=all;i++)if(dfn[i]==-1) Tarjan (i, I);//Notice the start point!!! 102 }103vector<int>G[n];104 intDu[n]; the voidSuodian () {106Memset (Du,0,sizeof(du));107 for(inti =1; I <= Taj; i++) g[i].clear ();108 for(inti =0; i < Edgenum; i++){109 intU = belong[edge[i]. from], V =belong[edge[i].to]; the if(u!=v)111 { theG[u].push_back (v), du[v]++;113 //printf ("%d%d\n", u,v); the } the } the }117 voidInit () {memset (head,-1,sizeof(head)); edgenum=0;}118 intMain ()119 { - //Read ();121 intt,a,b;122scanf"%d",&t);123 while(t--)124 { thescanf"%d",&n);126 init ();127flag=0; - while(1)129 { thescanf"%d%d",&a,&b);131 if(a==0&&b==0) Break; theAdd (A +1, B +1);133 }134 tarjan_init (n);135 Suodian ();136 137 if(taj==1&&flag==0) puts ("YES");138 ElsePuts"NO");139 } $ return 0;141}
hdu-3594 Cactus (Strong connectivity)