题目链接:###
题目分析:###
tarjan缩点后统计入度为0的连通分量。
代码:###
#include<bits/stdc++.h>
#define MAXN 100000+5
#define MAXM 500000+5
using namespace std;
inline int read(){
int cnt=0,f=1;char c;
c=getchar();
while(!isdigit(c)){
if(c=='-')f=-f;
c=getchar();
}
while(isdigit(c)){
cnt=cnt*10+c-'0';
c=getchar();
}
return cnt*f;
}
int n,dfn[MAXN],nxt[MAXM],first[MAXN],to[MAXM],low[MAXN],in[MAXN],id[MAXN];
int sign,sta[MAXN];bool insta[MAXN];
int tot,cnt,top;
void add(int x,int y){
nxt[++tot]=first[x];
first[x]=tot;
to[tot]=y;
}
void dfs(int u){
dfn[u]=low[u]=++sign;
sta[++top]=u,insta[u]=1;
for(register int i=first[u];i;i=nxt[i]){
int t=to[i];
if(!dfn[t]) dfs(t),low[u]=min(low[u],low[t]);
else if(insta[t]&&dfn[t]<low[u])low[u]=dfn[t];
}
if(low[u]==dfn[u]){
cnt++; do{
id[sta[top]]=cnt,insta[sta[top]]=0;
}while(sta[top--]!=u);
}
}
int ans=0;int m;
int main(){
n=read();m=read();
for(register int i=1;i<=m;i++){
int x=read();
int y=read();
add(x,y);
}
for(register int i=1;i<=n;i++)if(!dfn[i])dfs(i);
for(register int i=1;i<=n;i++)
for(register int j=first[i];j;j=nxt[j]){
int v=to[j];
if(id[v]!=id[i])in[id[v]]++;
}
for(register int i=1;i<=cnt;i++)if(!in[i])ans++;
printf("%d",ans);
return 0;
}