POJ 2186 Popular Cows

来源:互联网 发布:淘宝怎么开店好赚钱呢 编辑:程序博客网 时间:2024/05/29 13:53
http://poj.org/problem?id=2186
Popular Cows
Time Limit: 2000MSMemory Limit: 65536KTotal Submissions: 24347Accepted: 9992

Description

Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is 
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow. 

Input

* Line 1: Two space-separated integers, N and M 

* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular. 

Output

* Line 1: A single integer that is the number of cows who are considered popular by every other cow. 

Sample Input

3 31 22 12 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity. 

Source

USACO 2003 Fall
scc图中出度为0的顶点超过1个 那么就不可能存在 受所有牛喜爱的牛。。故输出0;当出度为0的顶点为1个,输出顶点所代表的那个强连通分支的顶点数即可
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b)
{
return a>b;
}
vector<int>G[maxn];
int lowlink[maxn],sccno[maxn],pre[maxn],dfs_clock,scc_cnt;
stack<int>s;
int n,m;
void dfs(int u)
{
pre[u]=lowlink[u]=++dfs_clock;
s.push(u);
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(!pre[v])
{
dfs(v);
lowlink[u]=min(lowlink[u],lowlink[v]);
}
else if(!sccno[v])lowlink[u]=min(lowlink[u],pre[v]);
}
if(lowlink[u]==pre[u])
{
scc_cnt++;
for(;;)
{
int x=s.top();s.pop();
sccno[x]=scc_cnt;
if(x==u)break;
}
}
}
void find_scc(int n)
{
dfs_clock=scc_cnt=0;
cle(sccno),cle(pre);
for(int i=1;i<=n;i++)
if(!pre[i])dfs(i);
}
void solve()
{
find_scc(n);
int outdegree[maxn];
cle(outdegree);
for(int i=1;i<=n;i++)
for(int j=0;j<G[i].size();j++)
{
int k=G[i][j];
if(sccno[i]!=sccno[k])
outdegree[sccno[i]]++;
}
int cnt=0,val;
for(int i=1;i<=scc_cnt;i++)
{
if(outdegree[i]==0)
{
val=i;
cnt++;
}
}
if(cnt>1)printf("%d\n",0);
else
{
int ans=0;
for(int i=1;i<=n;i++)
{
if(sccno[i]==val)ans++;
}
printf("%d\n",ans);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
//出度为0的点超过1个,则输出0;不然就输出出度为0的那个点所代表的那个强连通分支的顶点数即可。
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0;i<maxn;i++)
G[i].clear();
int a,b;
for(int i=1;i<=m;i++)
{
scanf("%d %d",&a,&b);
G[a].push_back(b);
}
solve();
}
return 0;
}

0 0
原创粉丝点击