只要找强联通分量大于一的个数就行
#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
struct E{int v,nxt;};
E edge[N];
int dfn[N],low[N],cnt,visnum,num[N],belong[N],head[N];
int n,m,vis[N];
stack<int> q;
void tarjan(int u){
dfn[u] = low[u] = ++visnum;
vis[u] = 1;
q.push(u);
for(int v,i=head[u];i;i=edge[i].nxt){
v = edge[i].v;
if(!vis[v]){
tarjin(v);
low[u] = min(low[u],low[v]);
}else low[u] = min(low[u],dfn[v]);
}
if(dfn[u]==low[u]){
++cnt;
int v;
do{
v = q.top();q.pop();
belong[v] = cnt;
num[cnt]++;
vis[v] = 0;
}while(u!=v);//回溯
}
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++){
int u,v;
scanf("%d %d",&u,&v);
edge[i].v = v; edge[i].nxt = head[u]; head[u] = i;
}
for(int i=1;i<=n;i++){
if(!dfn[i]){
tarjin(i);
}
}
int ans=0;
for(int i=1;i<=cnt;i++){
if(num[i]>1)ans++;
}printf("%d\n",ans);
return 0;
}