Portal: http://poj.org/problem?id=1236
Implementation code:
1#include <cstdio>2#include <algorithm>3#include <cstring>4#include <map>5 using namespacestd;6 7 Const intmaxn=10010;8 Const intmaxm=50010;9 Ten structedge{ One intto ; A intNext; - }EDGE[MAXM]; - the intHEAD[MAXN]; - inttot; - - voidAddedge (intUintv) { +edge[tot].to=v; -edge[tot].next=Head[u]; +head[u]=tot++; A } at - voidinit () { -memset (head,-1,sizeof(head)); -tot=0; - } - in intLOW[MAXN],DFS[MAXN],STACK[MAXN],NUM[MAXN],BELONG[MAXN]; - BOOLINSTACK[MAXN]; to intIndex; + intSCC; - inttop; the * voidTargan (intu) { $low[u]=dfs[u]=++Index;Panax Notoginsengstack[top++]=u; -instack[u]=true; the + intv; A for(inti=head[u];i!=-1; i=Edge[i].next) { thev=edge[i].to; + if(!Dfs[v]) { - Targan (v); $ if(low[u]>Low[v]) $low[u]=Low[v]; -}Else if(instack[v]&&low[u]>Dfs[v]) { -low[u]=Dfs[v]; the } - }Wuyi the if(low[u]==Dfs[u]) { -Scc++; Wu - Do{ Aboutv=stack[--top]; $instack[v]=false; -belong[v]=SCC; -num[scc]++; -} while(v!=u); A } + } the - voidSolve (intN) { $memset (Instack,false,sizeof(Instack)); thememset (DFS,0,sizeof(DFS)); thememset (Num,0,sizeof(Num)); the theindex=scc=top=0; - for(intI=1; i<=n;i++) in if(!Dfs[i]) the Targan (i); the } About themap<int,int>MP[MAXN]; the int out[MAXN],inch[MAXN]; the intMain () { + intN; - while(SCANF ("%d", &n)! =EOF) { the init ();Bayi the for(intI=1; i<=n;i++){ the intv; - while(SCANF ("%d", &v) &&v) { - Addedge (i,v); themp[i][v]=1; the } the } the Solve (n); - if(scc==1){ theprintf"1\n0\n"); the Continue; the }94 the for(intI=1; i<=n;i++) the for(intj=1; j<=n;j++){ the if(mp[i][j]&&belong[i]!=Belong[j]) {98 out[belong[i]]++; About inch[belong[j]]++; - }101 }102 103 intans1=0, ans2=0;104 for(intI=1; i<=scc;i++){ the if(inch[i]==0) ans1++;106 if( out[i]==0) ans2++;107 }108 109printf"%d\n%d\n", Ans1,max (ANS1,ANS2)); the 111 the }113}
POJ 1236 Network of schools