#include <cstdio>#include<vector>#include<cstring>using namespacestd;Const intmax_n=10005; Vector<int>g[max_n]; vector<int> Rg[max_n];//diagram after the storage edge reversedvector<int> postorder;//Post -sequential traversal of storage graphsintv,e;BOOLUsed[max_n];intComp[max_n];//stores the connected components that each node belongs tovoidAddedge (intUintv) {G[u].push_back (v); Rg[v].push_back (u);}voidDfsintu) {Used[u]=true; for(intI=0; I<g[u].size (); i++) { intv=G[u][i]; if(!Used[v]) Dfs (v); } postorder.push_back (U);}voidRdfsintUintk) {Used[u]=true; Comp[u]=K; for(intI=0; I<rg[u].size (); i++) { intv=Rg[u][i]; if(!Used[v]) Rdfs (v,k); }}intSCC () {memset (used,false,sizeof(used)); for(intI=1; i<=v;i++) { if(!Used[i]) DFS (i); } memset (Used,false,sizeof(used)); intk=0; for(intI=postorder.size ()-1; i>=0; i--) { intv=Postorder[i]; if(!Used[v]) Rdfs (v,k++); } returnK;}intMain () {scanf ("%d%d",&v,&E); for(intI=0; i<e;i++) { intu,v; scanf ("%d%d",&u,&v); Addedge (U,V); } intn=SCC (); intu=0, num=0; for(intI=1; i<=v;i++) { if(comp[i]==n-1)//the possible answer must be the last strong connected component{u=i; Num++; }} memset (used,false,sizeof(used)); Rdfs (U,0);//If a point in the last connected component can traverse all nodes by the opposite side, the Unicom component is the answer for(intI=1; i<=v;i++) { if(!Used[i]) {num=0; Break; }} printf ("%d\n", num); return 0;}
POJ (2186) strong connected component decomposition