原文引用https://www.dazhuanlan.com/2019/08/25/5d623a5f73d79/
神tm快读能超时,真是神奇123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
using namespace std;const int maxn=20000;int n,num=0,t_num=0,tot=0,ans=0;int insta[maxn],head[maxn],dfn[maxn],low[maxn],color[maxn],du[maxn];stack<int> s; int x=0,t=1;char ch=getchar(); while(ch>'9'||ch<'0'){if(ch=='-')t=-1;ch=getchar();} while(ch<='9'&&ch>='0') x=(x<<3)+(x<<1)+ch-'0',ch=getchar(); return x*t;}*/struct { int v,next;}e[maxn];void add(int u,int v){ e[++num].v=v; e[num].next=head[u]; head[u]=num;}void tarjan(int x){ dfn[x]=low[x]=++tot; s.push(x);insta[x]=1; for(int i=head[x];i;i=e[i].next) { int v=e[i].v; if(!dfn[v]) { tarjan(v); low[x]=min(low[x],low[v]); } else if(insta[v]) low[x]=min(low[x],dfn[v]); } int t; if(dfn[x]==low[x]) { t_num++; do { t=s.top(); insta[t]=0; color[t]=t_num; s.pop(); }while(x!=t); }}int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) { int p; scanf("%d",&p); while(p) { add(i,p); scanf("%d",&p); } } 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=e[j].next) { int v=e[j].v; if(color[i]!=color[v]) du[color[v]]++; } for(int i=1;i<=t_num;i++) if(!du[i]) ans++; printf("%dn",ans); return 0;}