Test instructions: Given a graph of n-point m edges, ask how many strongly connected component points are greater than or equal to 2.
Test instructions understand the basic there is no problem.
1#include <stdio.h>2#include <string.h>3#include <stack>4#include <queue>5 using namespacestd;6 7 Const intmaxn=1e4+5;8 Const intmaxm=5e4+5;9 Ten inthead[maxn],point[maxm],nxt[maxm],size; One intn,t,scccnt; A intSTX[MAXN],LOW[MAXN],SCC[MAXN]; - intNUM[MAXN]; -stack<int>S; the - voidinit () { -memset (head,-1,sizeof(head)); -Size=0; +memset (NUM,0,sizeof(num)); - } + A voidAddintAintb) { atpoint[size]=b; -nxt[size]=Head[a]; -head[a]=size++; - } - - voidDfsints) { instx[s]=low[s]=++T; - S.push (S); to for(intI=head[s];~i;i=Nxt[i]) { + intj=Point[i]; - if(!Stx[j]) { the Dfs (j); *low[s]=min (low[s],low[j]); $ }Panax Notoginseng Else if(!Scc[j]) { -low[s]=min (low[s],stx[j]); the } + } A if(low[s]==Stx[s]) { thescccnt++; + while(1){ - intu=S.top (); S.pop (); $scc[u]=scccnt; $num[scccnt]++; - if(S==u) Break; - } the } - }Wuyi the voidSETSCC () { -memset (STX,0,sizeof(STX)); Wumemset (SCC,0,sizeof(SCC)); -T=scccnt=0; About for(intI=1; i<=n;++i)if(!Stx[i]) DFS (i); $ } - - intMain () { - intm; Ascanf"%d%d",&n,&m); + init (); the while(m--){ - intb; $scanf"%d%d",&a,&b); the Add (A, b); the } the SETSCC (); the intans=0; - for(intI=1; i<=scccnt;++i)if(num[i]>1) ans++; inprintf"%d\n", ans); the return 0; the}
View Code
POJ3180 Strong Connectivity