## 【图论】[BZOJ 1051]受欢迎的牛

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <stack>
using namespace std;
const int MAXN = 10000;
const int MAXM = 50000;
struct node{
int v;
node *next;
++ecnt;
ecnt->v = v;
}
int dfn[MAXN+10], low[MAXN+10], dcnt, id[MAXN+10], kcnt, sz[MAXN+10];
bool insta[MAXN+10];
stack<int> sta;
void dfs(int u){
dfn[u]  = low[u] = ++dcnt; sta.push(u); insta[u] = true;
int v = p->v;
if(!dfn[v]){
dfs(v);
low[u]=  min(low[u], low[v]);
}else if(insta[v])
low[u] = min(low[u], dfn[v]);
}
if(dfn[u] == low[u]){
int tmp; ++kcnt;
do{
tmp = sta.top();
id[tmp] = kcnt;
sta.pop();
sz[kcnt]++;
insta[u] = false;
}while(tmp!=u);
}
}
bool ans[MAXN+10];
void solve(){
int n, m, u, v;
scanf("%d%d", &n, &m);
for(int i=1;i<=m;i++){
scanf("%d%d", &u, &v);
}
for(int i=1;i<=n;i++)
if(!dfn[i])
dfs(i);
//printf("%d\n", kcnt);
for(int i=1;i<=n;i++)
if(id[p->v] != id[i])
ans[id[i]] = true;
int Print = 0;
for(int i=1;i<=kcnt;i++)
if(!ans[i]){
if(Print){
printf("0\n");
return ;
}
Print = sz[i];
}
printf("%d\n", Print);
}
int main(){
solve();

return 0;
}

posted on 2015-07-24 10:20  JeremyGuo  阅读(86)  评论(0编辑  收藏

• 随笔 - 165
• 文章 - 0
• 评论 - 2
• 引用 - 0