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.
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.
* 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
- POJ 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- POJ 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- POJ 2186(Popular Cows)
- POJ 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- POJ 2186 Popular Cows
- POJ 2186: Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- POJ 2186 Popular Cows
- 1B. Spreadsheets
- HDU 5174 Ferries Wheel
- Tarjan算法
- POJ 1236 Network of Schools
- POJ 2553 The Bottom of a Graph
- POJ 2186 Popular Cows
- HDU 5050 Divided Land
- HDU 4027 Can you answer these queries?
- Kosaraju算法
- HDU 2063 过山车
- HDU 3861 The King’s Problem
- CF 293 A. Vitaly and Strings
- centos7 下双网卡如何配置静态IP
- Codeforces Round #295 (Div. 2)
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
港剧豆角网
港剧tv
天天看港剧
港剧app
街坊财神港剧粤语
看港剧app
港剧屋手机版
富贵门粤语港剧
好看的港剧
电视迷港剧盒子影
港剧大全
港剧吧
在线港剧
港剧排行榜
港剧免费看
tvb剧
港剧推荐
好看港剧
在线看港剧
港剧在线看
经典港剧
08港剧
香港连续剧
tvb影视
香港电视连续剧
香港tvb
tvb电视
香港偶像剧
解决师粤语版港剧
粤语屋在线看港剧
港务
厦门港务股票
黄埔港务分公司首页
000905厦门港务
黄埔港务分公司
重庆港务物流集团有限公司
曹妃甸实业港务有限公司
广西北部湾港务集团有限公司
舟山港务集团招聘
水富港务酒店
湖南省港务集团