WA????????? #include<bits/stdc++.h> using namespace std; const int N=1e4+5; const int M=5e4+5; int nxt[M], head[N], to[M], st[N], top; int col, de[N], sz[N]; int dfn[N], low[N], co[N]; int in[N], out[N]; int cnt=0, n, m, num=0; inline void add(int x, int y) { to[++cnt] = y; nxt[cnt] = head[x]; head[x] = cnt; } void Tarjan(int u) { dfn[u] = low[n] = ++num; st[++top] = u; for(int i=head[u];i;i=nxt[i]) { int v = to[i]; if(!dfn[v]) { Tarjan(v); low[u] = min(low[u],low[v]); } else if(!co[v]) low[u] = min(low[u],low[v]); } if(dfn[u]==low[u]) { ++col; while(st[top]!=u) { co[st[top]] = col; sz[col]++; --top; } sz[col]++; co[st[top]] = col; --top; } // cout<<u<<"\n"; } int main() { scanf("%d", &n); for(int i=1;i<=n;i++) { int u; while(~scanf("%d", &u)&&u) add(i,u); } for(int i=1;i<=n;i++) if(!dfn[i]) Tarjan(i); for(int i=1;i<=n;i++) { for(int j=head[i];j;j=nxt[j]) { if(co[i]==co[to[j]]) continue; out[co[i]]++; in[co[to[j]]]++; // cout<<out[co[i]]<<" "<<in[co[to[j]]]<<"\n"; } } int ans1=0, ans2=0; for(int i=1;i<=col;i++) { if(in[i]==0) ans1++; if(out[i]==0) ans2++; // cout<<ans1<<" "<<ans2<<"\n"; } if(col==1) printf("1\n0\n"); else printf("%d\n%d\n", ans1, max(ans1,ans2)); return 0; } 6549