poj1236
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=200; struct my{ int next; int v; }; my bian[maxn*maxn]; int adj[maxn],fa,cnt,n,dfn[maxn],dfsn,instack[maxn*maxn],top; int low[maxn],out[maxn],in[maxn],c[maxn]; bool inv[maxn]; void myinsert(int u,int v){ bian[++fa].v=v; bian[fa].next=adj[u]; adj[u]=fa; } void tarjan(int x){ low[x]=dfn[x]=++dfsn; instack[++top]=x; inv[x]=true; for (int i=adj[x];i;i=bian[i].next){ int v=bian[i].v; if(!dfn[v]){ tarjan(v); low[x]=min(low[x],low[v]); } else if(inv[v]){ low[x]=min(low[x],dfn[v]); } } if(low[x]==dfn[x]){ int y; cnt++; do{ y=instack[top--]; c[y]=cnt; inv[y]=false; }while(x!=y); } } int main(){ scanf("%d",&n); for (int i=1;i<=n;i++){ int u; while(scanf("%d",&u)&&u){ myinsert(i,u); } } for (int i=1;i<=n;i++){ if(!dfn[i]){ tarjan(i); } } for (int i=1;i<=n;i++){ for (int j=adj[i];j;j=bian[j].next){ int v=bian[j].v; if(c[i]!=c[v]){ out[c[i]]++; in[c[v]]++; } } } int inans=0,outans=0; for (int i=1;i<=cnt;i++){ if(in[i]==0) inans++; if(out[i]==0) outans++; } printf("%d\n",inans); if(cnt==1) printf("0\n"); else printf("%d\n",max(inans,outans)); return 0; }