Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 20718 | Accepted: 8438 |
Description
Input
Output
Sample Input
3 3 1 2 2 1 2 3
Sample Output
1
Hint
Source
#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #define N 10510 #define E 100000 #define M 1000000 using namespace std; int head[N],sta[M],num[N],re,ans,next[E],id[N],in[N],vec[E],vis[N],dfn[N],low[N],clock_m,edge_m; int addedge(int s,int e){ vec[edge_m]=e;next[edge_m]=head[s];head[s]=edge_m++; } int init(){ memset(vis,0,sizeof(vis)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(num,0,sizeof(num)); memset(head,-1,sizeof(head)); memset(id,0,sizeof(id)); memset(in,0,sizeof(in)); edge_m=0;clock_m=0;ans=0;re=0; } int tarjan(int x){ dfn[x]=low[x]=++clock_m; sta[++ans]=x; vis[x]=1; for(int i=head[x];i!=-1;i=next[i]){ int goal=vec[i]; if(!dfn[goal]){ tarjan(goal); low[x]=min(low[x],low[goal]); } else if(/*vis[goal]*/!id[goal]) low[x]=min(low[x],dfn[goal]); } if(low[x]==dfn[x]){ re++;int v; do{ v=sta[ans--];vis[v]=0;num[re]++;id[v]=re; }while(v!=x); } return 1; } int main() { int tcase,n,m,s,e; while(scanf("%d%d",&n,&m)!=EOF) { //system("PAUSE"); init(); for(int i=0;i<m;i++){ scanf("%d%d",&s,&e); addedge(s,e); } for(int i=1;i<=n;i++) if(!dfn[i]) { tarjan(i); } for(int i=1;i<=n;i++){ for(int j=head[i];j!=-1;j=next[j]){ if(id[vec[j]]!=id[i]){ in[id[i]]++; } } } ans=0;int k=1; for(int i=1;i<=re;i++){ if(in[i]==0){ sta[ans++]=i;k=i; } } if(ans>1)printf("0\n"); else printf("%d\n",num[k]); } return 0; }