1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> #define rep(i,x,y) for(register int i=x;i<=y;++i) #define repd(i,x,y) for(register int i=x;i>=y;--i) #define ll long long using namespace std; const int N=1e5; struct { int v,nxt; inline void init(int a,int b){v=a;nxt=b;} }e[N]; int dfn[N],low[N],in[N],ans,cnt,head[N],n,m,stck[N],indx; inline void adde(int a,int b){ e[cnt].init(b,head[a]); head[a]=cnt++; }
void tarjan(int u){ in[u]=1; low[u]=dfn[u]=++cnt; stck[++indx]=u; for(int i=head[u],v;i!=-1;i=e[i].nxt){ if(!dfn[v=e[i].v]){tarjan(v); low[u]=min(low[u],low[v]);} else if(in[v])low[u]=min(low[u],dfn[v]); } if(low[u]==dfn[u]){ int k=0; do{ in[indx]=0; --indx; k++; }while(stck[indx+1]!=u); if(k>1)ans++; } }
int main(){ scanf("%d%d",&n,&m); memset(head,-1,sizeof(head)); rep(i,1,m){ int x,y; scanf("%d%d",&x,&y); adde(x,y); } cnt=0; rep(i,1,n){ if(!dfn[i])tarjan(i); } printf("%dn",ans); return 0; }
|
近期评论