Before the Kosaraju_algorithm understand the wrong, now thoroughly understand.
Kosaraju_algorithm:
• step1: Depth- first traversal of the original G, recording the departure time of each node. Formed a
forest (a lot of trees). • Step2: Select the vertices that have the latest departure time, traverse the inverse of the graph GT, and Delete the vertices that can traverse to form a strong connected component. • Step3: If there are vertices not removed, continue to Step2, otherwise the algorithm ends. for this topic, the answer to the first question is the number of nodes after the indentation is 0, the second question is Max (the number of nodes in the 0, the number of nodes entering and leaving 0)If there is only one strong connected component, then output 1 0 is possible.
#include <cstdio>#include<cstring>#include<cmath>#include<vector>#include<algorithm>using namespacestd;Const intmaxn= the;intdfn[maxn],u[100000],v[100000],BELONG[MAXN];intIN[MAXN],OUT[MAXN],FLAG[MAXN];structpoint{intID,DFN;} Point[maxn];vector<int>G[maxn];vector<int>FG[MAXN];intN,tot;intSum,block,inch, out;voidinit () { for(intI=0; i<maxn; i++) g[i].clear (); for(intI=0; i<maxn; i++) fg[i].clear (); memset (DFN,0,sizeofDFN); Memset (In,0,sizeofIn ); memset (out,0,sizeofOut ); memset (Flag,0,sizeofflag); Tot=0;//time Stampsum=0;//the number of edgesblock=0;//number of strongly connected components inch=0;//number of points with a count of 0 in degrees out=0;//number of points with a statistical degree of 0}BOOLcmpConstPoint&a,Constpoint&b) { returnA.dfn>B.DFN;}voidDfs (intNow ) {Flag[now]=1; for(intI=0; I<g[now].size (); i++) if(!Flag[g[now][i]]) Dfs (G[now][i]); Tot++; Dfn[now]=Tot;}voidDfsintNow ) {Belong[now]=Block; for(intI=0; I<fg[now].size (); i++) if(!Belong[fg[now][i]]) DFS (fg[now][i]);}intMain () { while(~SCANF ("%d",&N)) {init (); for(intI=1; i<=n; i++) { while(1) { intv; scanf ("%d",&v); if(!V) Break; Sum++; U[sum]=i; V[sum]=v; G[i].push_back (v); Fg[v].push_back (i); } } for(inti =1; I <= N; i++)if(!Flag[i]) Dfs (i); for(intI=0; i<n; i++) Point[i].id=i+1, point[i].dfn=dfn[i+1]; Sort (Point,point+n,cmp); for(intI=0; i<n; i++) if(!Belong[point[i].id]) Block++, DFS (point[i].id); for(intI=1; i<=sum; i++) if(belong[u[i]]!=Belong[v[i]]) Out[belong[u[i] ]++,in[belong[v[i]]]++; for(intI=1; i<=block; i++) { if(! In[i])inch++; if(! Out[i]) out++; } if(block==1) printf ("1\n0\n"); Elseprintf"%d\n%d\n",inch, Max (inch, out)); } return 0;}
POJ 1236 Network of schools