qscqesze
Published on 2017-09-02 11:31 in 暂未分类 with qscqesze

# 1051: [HAOI2006]受欢迎的牛

Time Limit: 20 Sec

Memory Limit: 256 MB

## 题目连接

http://www.lydsy.com/JudgeOnline/problem.php?id=1051

## Description

Input

Output

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

## HINT

#include<iostream>
#include<stdio.h>
#include<vector>
#include<cstring>
using namespace std;
#define maxn 10005
vector<int> E[maxn];
vector<int> rE[maxn];
int belong[maxn];
int vis[maxn];
int cl[maxn];
int cnt = 0;
int siz[maxn];
int kis[maxn];
void dfs1(int x)
{
vis[x]=1;
for(int i=0;i<E[x].size();i++)
{
int e = E[x][i];
if(vis[e])continue;
dfs1(e);
}
cl[++cnt]=x;
}
void dfs2(int x)
{
vis[x]=1;
belong[x]=cnt;
siz[cnt]++;
for(int i=0;i<rE[x].size();i++)
{
int e = rE[x][i];
if(vis[e])continue;
dfs2(e);
}
}
int main()
{
int n,m;scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int x,y;scanf("%d%d",&x,&y);
E[x].push_back(y);
rE[y].push_back(x);
}
for(int i=1;i<=n;i++)
{
if(!vis[i])
dfs1(i);
}
memset(vis,0,sizeof(vis));
cnt = 0;
for(int i=n;i>=1;i--)
{
if(!vis[cl[i]])
{
cnt++;
dfs2(cl[i]);
}
}
for(int i=1;i<=n;i++)
{
for(int j=0;j<E[i].size();j++)
{
if(belong[i]==belong[E[i][j]])
continue;
kis[belong[i]]++;
}
}
int ans1=0,ans2=0;
for(int i=1;i<=cnt;i++)
{
if(kis[i]==0)
{
ans1++;
ans2=i;
}
}
if(ans1==1)
printf("%d\n",siz[ans2]);
else
puts("0");
}

posted @ 2015-11-05 21:59  qscqesze  阅读(135)  评论(0编辑  收藏