(Likely to stay when the winter training camp to the bar, the result was I mutter to now ...
Tarjan simple application, which is very simple to see.
FIG star cows then only one of the components is a strong link 0.
If there are two or more of the components of a strong link 0, then there is no star cows, because these can not pass the degree of "love" strongly connected components between 0
So, the final answer is the number of cows Unicom strong component in!
#include<cstdio>
#include<algorithm>
#include<queue>
#include<stack>
#define ll long long
using namespace std;
inline ll read()
{
ll sum = 0, p = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-')
p = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9')
{
(sum *= 10) += ch - '0';
ch = getchar();
}
return sum * p;
}
const int maxn = 10005,maxm = 50005;
struct edge
{
int nxt,to;
}e[maxm];
int n,m,cnt,tot,ncnt;
int dfn[maxn],low[maxn],mrk[maxn],id[maxn],du[maxn],head[maxn];
bool vis[maxn];
stack<int> q;
void add(int a,int b)
{
e[++cnt].to = b;
e[cnt].nxt = head[a];
head[a] = cnt;
}
void tarjan(int x)
{
dfn[x] = low[x] = ++tot;
q.push(x);
vis[x] = true;
for(int i = head[x];i;i = e[i].nxt)
{
int v = e[i].to;
if(!dfn[v])
{
tarjan(v);
low[x] = min(low[x],low[v]);
}
else if(vis[v])
low[x] = min(low[x],dfn[v]);
}
int k;
if(dfn[x] == low[x])
{
ncnt++;
do
{
k = q.top();
q.pop();
vis[k] = false;
id[k] = ncnt;
mrk[ncnt]++;
}while(x != k);
}
}
int main()
{
n = read(),m = read();
for(int i = 1;i <= m;i++)
{
int a = read(),b = read();
add(a,b);
}
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;j = e[j].nxt)
{
int v = e[j].to;
if(id[i] != id[v])
du[id[i]]++;
}
int ans = 0;
for(int i = 1;i <= ncnt;i++)
if(!du[i])
{
if(ans)
{
printf("0\n");
return 0;
}
ans = i;
}
printf("%d\n",mrk[ans]);
return 0;
}