# p2341&bzoj1051 受欢迎的牛

Input

Output

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

HINT

100%的数据N<=10000,M<=50000

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
int dfn[110000],low[110000],star[110000],cnt,sum,belong[110000];
int num[110000],isr[110000];
vector<int>v[110000];
stack<int>a;
stack<int>b;
queue<int>q;
void tarjan(int x){
low[x]=dfn[x]=++cnt;
a.push(x);
isr[x]=1;
int i,j,k;
for(i=0;i<v[x].size();i++)
if(!dfn[v[x][i]]){
tarjan(v[x][i]);
low[x]=min(low[x],low[v[x][i]]);
}else if(isr[x]){
low[x]=min(low[x],dfn[v[x][i]]);
}
if(dfn[x]==low[x]){
int tot=0;
sum++;
while(1){
int u=a.top();
belong[u]=sum;
b.push(u);
tot++;
a.pop();
isr[u]=0;
if(u==x)break;
}
while(!b.empty()){
star[sum]=tot;
b.pop();
}
}
}
int main()
{     int n,m,i,j,k,x,y;
cin>>n>>m;
for(i=1;i<=m;i++){
cin>>x>>y;
v[x].push_back(y);
}
for(i=1;i<=n;i++)
if(!dfn[i])tarjan(i);
for(i=1;i<=n;i++)
for(j=0;j<v[i].size();j++)
if(belong[v[i][j]]!=belong[i]){
num[belong[i]]++;
}
int wh,tot=0;
for(i=1;i<=sum;i++)
if(!num[i]){
tot++;
wh=i;
}
if(tot!=1){
cout<<0<<endl;
return 0;
}
cout<<star[wh]<<endl;
return 0;
}

posted @ 2018-05-15 10:34  水题收割者  阅读(59)  评论(0编辑  收藏