# BZOJ1654 奶牛舞会+tarjan算法（入门题目）+判断图中有几个环

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int num=10010;
/******************/
struct node{
int u,v,next;
}e[num*5];
/******************/
int dfn[num],low[num],ind,t,stack[num],cnt3,f[num],w[num];
bool vis[num];
/******************/
int n,m,ans;
/******************/
void int_i(void)
{
cnt=-1;
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
ind=0;
t=0;
cnt3=0;
memset(w,0,sizeof(w));
return ;
}
{
e[++cnt].v=v;
e[cnt].u=u;
return ;
}
void tarjan(int u)
{
int v;
dfn[u]=low[u]=++ind;
stack[++t]=u;
vis[u]=true;
{
v=e[i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else {
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u])
{
cnt3++;
do{
v=stack[t--];
vis[v]=false;
f[v]=cnt3;
w[cnt3]++;
}while(v!=u);
if(w[cnt3]>1) ans++;
}
return ;
}
int main()
{
int u,v;
int_i();
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
}
for(int i=1;i<=n;i++)
{
if(!dfn[i]){
tarjan(i);
}
}
printf("%d\n",ans);
return 0;
}

/*
5 4
2 4
3 5
1 2
4 1
*/


09-14 35

11-18 41
04-14 435
08-09 2万+
07-10 1万+
11-25 6419
08-29 394
05-22 356
09-16 20
06-16 40