POJ 2762
Test instructions: Let me give you a diagram asking if you have a path from X to Y or from Y to X for any two points x, y?
Analysis: Is this obviously a one-way connectivity diagram? We can indent the strongly connected component, and the graph after the contraction point must be a direction-free graph; Now, the question is equivalent to giving you a undirected graph, asking if it is a one-way connected graph. In this way, we can arrange the topological ordering of the undirected undirected graph, and if there is an edge between any adjacent two points in the resulting topological sequence, that is, topo[i]--topo[i+1], the undirected graph is a unidirectional connected graph.
1#include <cstdio>2#include <cstring>3#include <queue>4#include <algorithm>5 #define_CLR (x, y) memset (x, y, sizeof (x))6 #defineINF 0x3f3f3f3f7 #defineN 10108 using namespacestd;9 Ten structNode One { A intto, next; -}edge[n*6]; - inthead[n], tot; the intDfn[n], low[n]; - intBleg[n], sta[n]; - BOOLInstack[n]; - intcnt, top, ght, N; + - voidInit () + { Acnt=tot=ght=top=0; at_CLR (Head,-1); -_CLR (DFN,0); -_CLR (Instack,0); - } - - voidAdd_edge (intAintb) in { -Edge[tot].to =b; toEdge[tot].next =Head[a]; +Head[a] = tot++; - } the * voidDfsintu) $ {Panax Notoginsengdfn[u]=low[u]=++CNT; -sta[top++] =u; theInstack[u] =true; + for(intI=head[u]; i!=-1; I=edge[i].next) A { the intv =edge[i].to; + if(!Dfn[v]) - { $ Dfs (v); $Low[u] =min (Low[u], low[v]); - } - Else if(Instack[v]) low[u] =min (Low[u], dfn[v]); the } - if(low[u]==Dfn[u])Wuyi { theght++; - //printf ("Num:%d\n", ght); Wu intv; - Do About { $v = sta[--top]; - //printf ("%d", v); -INSTACK[V] =false; -BLEG[V] =ght; A} while(u!=v); + //puts (""); the } - } $ the intInde[n], mat[n][n]; the voidTarjan () the { the for(intI=1; i<=n; i++) - if(!Dfn[i]) DFS (i); in the_CLR (Inde,0); the_CLR (Mat,0); About //to establish a new direction-free graph and to find the degree of each point the for(intu=1; u<=n; u++) the for(intI=head[u]; i!=-1; I=edge[i].next) the { + intv =edge[i].to; - if(Bleg[v]! =Bleg[u]) the {Bayiinde[bleg[v]]++; theMAT[BLEG[U]][BLEG[V]] =1; the } - } - } the the intTopo[n], k=0; the voidTopsort () the { -queue<int>Q; the for(intI=1; i<=ght; i++) the if(inde[i]==0) Q.push (i); the //printf ("Topsort: \ n");94k=0; the while(!q.empty ()) the { the intU =Q.front (); Q.pop ();98 //printf ("%d", u); AboutTOPO[++K] =u; - for(intI=1; i<=ght; i++)101 {102 if(!mat[u][i])Continue;103inde[i]--;104 if(inde[i]==0) Q.push (i); the }106 }107 //puts ("");108 }109 the voidSolved ()111 { the BOOLTag =true;113 //printf ("k=%d\n", k); the for(intI=1; i<k; i++) the if(!mat[topo[i]][topo[i+1]])//If there are two adjacent points between the topological sequences, the non-unidirectional connected graphs the {117Tag =false;118 Break;119 } -Puts (tag?)"Yes":"No");121 }122 123 intMain ()124 { the intT, M, A, b;126scanf"%d", &T);127 while(t--) - {129 Init (); thescanf"%d%d", &n, &m);131 while(m--) the {133scanf"%d%d", &a, &b);134 Add_edge (A, b);135 }136 Tarjan ();137 Topsort ();138 Solved ();139 } $ return 0;141}
View Code
One-way connectivity of graphs