# poj 2186 （强连通缩点传递关系）

#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<queue>
#include <iomanip>
#include<iostream>
#include<algorithm>
using namespace std ;
const  int N= 11111 ;
const  int M= 55555 ;
struct node
{
int u ,v,next;
}edge[M] ;
int top ,sum,cnt,dep ;

{
edge[top].u=u;
edge[top].v=v;
}

void tarjan(int u )
{
low[u]=dfn[u]=++dep;
stack[cnt++]=u;
vist[u]=1;
for(int i = head[u] ; i!=-1 ; i=edge[i].next)
{
int  v= edge[i].v ;
if(!dfn[v])
{
tarjan(v) ;
low[u] = min(  low[u] , low[v] ) ;
}else if(vist[v])
low[u] = min(  low[u] , dfn[v] );
}
if(low[u] == dfn[u])
{
int x;
sum++ ;
do
{
x=stack[--cnt] ;
vist[x] = 0 ;
belong[x]=sum;
}while(x!=u) ;
}
}

int main()
{
int n,m,u,v ;
while(~scanf("%d%d",&n,&m))
{
top  = sum = dep = cnt = 0;
memset(out,0,sizeof(out));
memset(belong,0,sizeof(belong));
memset(low,0,sizeof(low)) ;
memset(dfn,0,sizeof(dfn)) ;
memset(vist,0,sizeof(vist)) ;
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*  ",sum);
for(int i = 1 ; i <= n ; i++)
for(int j = head[i] ; j!=-1; j=edge[j].next)
{
int  v = edge[j].v ;
if(belong[i] != belong[v])
{
out[belong[i]]++;
}
}
int flag = 0 , k ;
for(int i = 1 ; i <= sum ; i++)
if(out[i] == 0  )
{
k=i ;
flag++;
if(flag > 1)
break ;
}

if( flag > 1 )
{
printf("0\n");
}
else
{
int ans = 0 ;
for(int i = 1 ; i <= n ; i++)
if(belong[i] == k)
ans++;
printf("%d\n",ans);
}
}
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120