Refer to this blog:
http://blog.csdn.net/ascii991/article/details/7466278
#include <stdio.h>#include<string.h>#include<algorithm>#include<math.h>#include<vector>#include<stack>using namespaceStd;typedefLong LongLL;Const intN = 1e2+5;intHead[n],tot,p,h[n],n, out[N],inch[N];structedge{intU,v,next;} Edge[n*n],e[n*N];voidAddintUintv) {edge[tot].u=u; EDGE[TOT].V=v; Edge[tot].next=Head[u]; Head[u]=tot++;}voidAddedge (intUintv) {E[P].V=v; E[p].next=H[u]; H[u]=p++;}intClk,dfn[n],low[n],cnt,bel[n];BOOLInstack[n];stack<int>s;voidTargin (intu) {Dfn[u]=low[u]=++CLK; S.push (U); Instack[u]=true; for(intI=head[u];~i;i=Edge[i].next) { intv=edge[i].v; if(!Dfn[v]) {Targin (v); Low[u]=min (low[u],low[v]); } Else if(Instack[v]) Low[u]=min (low[u],dfn[v]); } if(dfn[u]==Low[u]) { ++CNT; intK; Do{k=S.top (); S.pop (); INSTACK[K]=false; BEL[K]=CNT; } while(k!=T); } }intMain () {scanf ("%d",&N); memset (Head,-1,sizeof(head)); memset (H,-1,sizeof(h)); for(intI=1; i<=n;++i) { for(intx;;) {scanf ("%d",&x); if(!x) Break; Add (i,x); } } for(intI=1; i<=n;++i)if(!Dfn[i]) targin (i); if(cnt==1) {printf ("1\n0\n"); return 0; } for(intI=0; i<tot;++i) { intK1=bel[edge[i].u],k2=BEL[EDGE[I].V]; if(K1==K2)Continue; Addedge (K1,K2); ++inch[K2]; ++ out[K1]; } intans1=0, ans2=0; for(intI=1; i<=cnt;++i) { if(!inch[i]) ++ans1; if(! out[i]) ++Ans2; } printf ("%d\n%d\n", Ans1,max (ANS1,ANS2)); return 0;}
View Code
POJ 1236 Network of schools strongly connected components of the graph