.
#include <iostream>#include<stack>#include<string.h>#include<stdlib.h>#include<algorithm>#include<math.h>#include<stdio.h>#defineN 1001using namespacestd;structnode{intX,y,next;} eg[100001];intN,tt,cnt,time,ins[n],head[n],low[n],dfn[n],be[n],inch[n],ch[n],map[n][n];voidinit () {TT=cnt=0; memset (Head,-1,sizeof(head)); memset (INS,0,sizeof(INS)); memset (inch,0,sizeof(inch)); memset (CH,0,sizeof(CH)); memset (Map,0,sizeof(map)); memset (BE,0,sizeof(BE));}voidAddintXxintyy) {eg[tt].x=xx; Eg[tt].y=yy; Eg[tt].next=HEAD[XX]; HEAD[XX]=tt++;} Stack<int>Q;voidTarjan (inti) { intW; Low[i]=dfn[i]=++Time ; Q.push (i); Ins[i]=1; for(intJ=head[i]; j!=-1; j=Eg[j].next) {W=eg[j].y; if(!Dfn[w]) {Tarjan (w); Low[i]=min (low[i],low[w]); } Else if(Ins[w]) {Low[i]=min (low[i],dfn[w]); } } if(dfn[i]==Low[i]) {CNT++; Do{W=Q.top (); Q.pop (); INS[W]=0; BE[W]=CNT; } while(!q.empty () &&i!=W); }}voidsolve () {memset (DFN,0,sizeof(DFN)); time=0; for(intI=1; i<=n; i++) { if(!Dfn[i]) {Tarjan (i); } } if(cnt==1) {printf ("1\n0\n"); return ; } for(intI=1; i<=n; i++) { for(intj=1; j<=n; J + +) { if(map[i][j]&&be[i]!=Be[j]) { inch[be[j]]++; Ch[be[i]]++; } } } intsum=0; intCount=0; for(intI=1; i<=cnt; i++) { if(inch[i]==0) sum++; if(ch[i]==0) count++; } printf ("%d\n%d\n", Sum,max (Sum,count)); return ;}intMain () {intxx; while(SCANF ("%d", &n)! =EOF) {init (); while(!Q.empty ()) {Q.pop (); } for(intI=1; i<=n; i++) { while(SCANF ("%d", &xx)!=eof&&xx!=0) {Map[i][xx]=1; Add (I,XX); }} solve (); } return 0;}
Poj1236:network of schools