Topic Links:
http://www.lydsy.com/JudgeOnline/problem.php?id=1051
Exercises
Strongly connected indents get the DAG graph, transpose the graph, and run DFS on the zero-in point to see if you can access all the points.
Code:
#include <iostream>#include<cstdio>#include<vector>#include<stack>#include<algorithm>#include<cstring>using namespacestd;Const intMAXN =10000+Ten;Const intINF =0x3f3f3f3f; Vector<int>G[MAXN],G2[MAXN];intPRE[MAXN], LOWLINK[MAXN], SCCNO[MAXN], Dfs_clock, scc_cnt;intInd[maxn],siz[maxn],vis[maxn];stack<int>S;intn,m;voidDfsintu) {Pre[u]= Lowlink[u] = + +Dfs_clock; S.push (U); for(inti =0; I < g[u].size (); i++) { intv =G[u][i]; if(!Pre[v]) {DFS (v); Lowlink[u]=min (Lowlink[u], lowlink[v]); } Else if(!Sccno[v]) {Lowlink[u]=min (Lowlink[u], pre[v]); } } if(Lowlink[u] = =Pre[u]) {scc_cnt++; intCNT =0; for (;;) {CNT++; intx =S.top (); S.pop (); SCCNO[X]=scc_cnt; if(x = = u) Break; } siz[scc_cnt]=CNT; }}voidFIND_SCC (intN) {dfs_clock= SCC_CNT =0; memset (Sccno,0,sizeof(SCCNO)); memset (PRE,0,sizeof(pre)); for(inti =0; I < n; i++)if(!Pre[i]) DFS (i); //for (int i = 0; i < n; i++) printf ("sccno[%d]:%d\n", I, Sccno[i]);}voidDFS2 (intu) {if(Vis[u])return; Vis[u]=1; for(inti =0; I < g2[u].size (); i++) { intv =G2[u][i]; DFS2 (v); }}voidinit () { for(inti =0; I < n; i++) G[i].clear (), g2[i].clear (); memset (Ind,0,sizeof(Ind)); memset (Vis,0,sizeof(Vis));}intMain () { while(SCANF ("%d%d", &n,&m) = =2&&N) {init (); for(inti =0; I < m; i++) { intu, v; scanf ("%d%d", &u, &v); U--, v--; G[u].push_back (v); } FIND_SCC (n); for(inti =0; I < n; i++) { for(intj =0; J < G[i].size (); J + +) { intv =G[i][j]; if(Sccno[i]! =Sccno[v]) {G2[sccno[v]].push_back (sccno[i]); Ind[sccno[i]]++; } } } intRT =-1; for(inti =1; I <= scc_cnt; i++) { if(Ind[i] = =0) RT =i; } DFS2 (RT); intSu =1; for(inti =1; I <= scc_cnt; i++) { if(!Vis[i]) {su=0; Break; } } if(su) {printf ("%d\n", Siz[rt]); } Else{printf ("0\n"); } } return 0;}
Bzoj 1051: [HAOI2006] Popular ox-strong connectivity Pinch point