提交时间:2024-01-02 13:36:08
运行 ID: 118911
#include<bits/stdc++.h> using namespace std; const int N=2e6+10; struct node{ int to,nxt; }edge[N]; int n,m,head[N],tot,rd[N],tp[N],ans; queue<int> q; void add(int u,int v){ edge[++tot].to=v; edge[tot].nxt=head[u]; head[u]=tot; } int main(){ scanf("%d%d",&n,&m); for(register int i=1;i<=m;i++){ int u,v; scanf("%d%d",&u,&v); add(u,v); rd[v]++; } for(register int i=1;i<=n;i++){ if(!rd[i])q.push(i); tp[i]=1; } while(!q.empty()){ int u=q.front(); q.pop(); for(register int i=head[u];i;i=edge[i].nxt){ int v=edge[i].to; tp[v]=tp[u]+1; rd[v]--; if(!rd[v])q.push(v); } } for(register int i=1;i<=n;i++) if(tp[i]==1||tp[i]==2)ans++; printf("%d\n",ans); return 0; }