Give n points, M edge, ask whether any two points u,v, whether meet u can reach V, or V can reach U
When you write, think of a pinch, and then judge if you can sort the topology.
But--wa---
Then I read the puzzle.
Http://edward-mj.com/archives/27
According to the Purple book, if there is a forward ring in the diagram, there is no topological ordering, and conversely there is
So the above image is for topological sequencing.
But because U,v's penetration is not 0,u,v between
So the graph after the end of the contraction point should be satisfied is a long chain.
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <stack>6#include <queue>7#include <vector>8 using namespacestd;9 Ten Const intMAXN =20005; One intFIRST[MAXN]; A intLOW[MAXN],PRE[MAXN],SC[MAXN]; - int inch[MAXN],DOUT[MAXN]; - intn,m; the intEcnt,scnt,dfs_clock; -stack<int>S; - -vector<int>G[MAXN]; + intC[MAXN]; - intANS[MAXN]; + A structedge{ at intV,next; -}e[3*MAXN]; - - voidinit () { -ECNT =0; -memset (first,-1,sizeof(first)); inMemsetinch,0,sizeof(inch)); -memset (Dout,0,sizeof(dout)); to } + - voidAddedges (intUintv) { theE[ECNT].V =v; *E[ecnt].next =First[u]; $First[u] = ecnt++;Panax Notoginseng } - the voidDfsintu) { +Low[u] = Pre[u] = + +Dfs_clock; A s.push (u); the for(inti = First[u];~i;i =E[i].next) { + intv =e[i].v; - if(!Pre[v]) { $ Dfs (v); $Low[u] =min (low[u],low[v]); - } - Else if(!sc[v]) low[u] =min (low[u],pre[v]); the } - if(Pre[u] = =Low[u]) {Wuyiscnt++; the for(;;) { - intx =S.top (); S.pop (); WuSC[X] =scnt; - if(x = = u) Break; About } $ } - } - - voidFIND_SCC () { A while(!S.empty ()) S.pop (); +memset (Low,0,sizeof(low)); memset (PRE,0,sizeof(pre)); theMemset (SC,0,sizeof(SC)); -Dfs_clock = SCNT =0; $ for(inti =1; I <= n;i++)if(!Pre[i]) DFS (i); the } the the BOOLTopsort (intN) { thequeue<int>s; - for(inti =1; I <= N; i++) { in if(!inch[i]) s.push (i); the if(s.size () = =2)return false; the } About while(!S.empty ()) { the intU =S.front (); S.pop (); the BOOLFlag =false; the for(inti =0; I < g[u].size (); i++) { + intv =G[u][i]; - inch[V]-=1; the if(!inch[v]) {Bayi if(flag)return false; the S.push (v); theFlag =true; - } - } the } the return true; the } the - the intMain () { the intT; thescanf"%d",&T);94 while(t--){ thescanf"%d%d",&n,&m); the init (); the for(inti =1; I <= m;i++){98 intu,v; Aboutscanf"%d%d",&u,&v); - addedges (u,v);101 }102 FIND_SCC ();103 if(SCNT = =1) {104Puts"Yes"); the Continue;106 }107 for(inti =1; I <= scnt;i++) g[i].clear ();108 for(intU =1; u <= n;u++){109 for(inti = First[u];~i;i =E[i].next) { the intv =e[i].v;111 if(Sc[u]! =Sc[v]) { the G[sc[u]].push_back (Sc[v]);113 inch[sc[v]]++; the } the } the }117 if(Topsort (SCNT)) puts ("Yes");118 ElsePuts"No");119 } - return 0;121}
View Code
POJ 2762 going from U-V or from V to u? "Strong Connectivity topology sequencing"