您的位置:首页 > 运维架构

POJ 2186 Popular Cows

2016-04-21 18:59 471 查看
http://poj.org/problem?id=2186Popular Cows
Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 24347Accepted: 9992
DescriptionEvery 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 Input3 3
1 2
2 1
2 3
Sample Output1
HintCow 3 is the only cow of high popularity. 
SourceUSACO 2003 Fallscc图中出度为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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: