Popular Cows(POJ 2186)
- 原题如下:
Popular Cows
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 40746 Accepted: 16574 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 3
1 2
2 1
2 3Sample Output
1
Hint
Cow 3 is the only cow of high popularity. - 题解:建图显然,假设两头牛A和B都被其他所有牛认为是红人,那么显然A和B互相认为对方是红人,即存在一个包含A、B两个顶点的圈,或者说,A、B同属于一个强连通分量,反之,如果一头牛被其他所有牛认为是红人,那么其所属的强连通分量内的所有牛都被其他所有牛认为是红人。由此可知,把图进行强连通分量分解后,至多有一个强连通分量满足题目的条件。而进行强连通分解时,我们还可以得到各个强连通分量拓扑排序后的顺序,唯一可能成为解的只有拓扑序最后的强连通分量,,所以在最后,我们只要检查最后一个强连通分量是否从所有顶点可达就好了。该算法的复杂度为O(N+M)。
- 代码:
#include <cstdio>
#include <stack>
#include <vector>
#include <algorithm>
#include <cstring> using namespace std; stack<int> s;
const int MAX_V=;
bool instack[MAX_V];
int dfn[MAX_V];
int low[MAX_V];
int ComponentNumber=;
int index;
vector<int> edge[MAX_V];
vector<int> redge[MAX_V];
vector<int> Component[MAX_V];
int inComponent[MAX_V];
int N, M;
bool visited[MAX_V]; void add_edge(int x, int y)
{
edge[x].push_back(y);
redge[y].push_back(x);
} void tarjan(int i)
{
dfn[i]=low[i]=index++;
instack[i]=true;
s.push(i);
int j;
for (int e=; e<edge[i].size(); e++)
{
j=edge[i][e];
if (dfn[j]==-)
{
tarjan(j);
low[i]=min(low[i], low[j]);
}
else
if (instack[j]) low[i]=min(low[i], dfn[j]);
}
if (dfn[i]==low[i])
{
ComponentNumber++;
do
{
j=s.top();
s.pop();
instack[j]=false;
Component[ComponentNumber].push_back(j);
inComponent[j]=ComponentNumber;
}
while (j!=i);
}
} void rdfs(int v)
{
visited[v]=true;
for (int i=; i<redge[v].size(); i++)
{
if (!visited[redge[v][i]])
{
rdfs(redge[v][i]);
}
}
} int main()
{
memset(dfn, -, sizeof(dfn));
scanf("%d %d", &N, &M);
for (int i=; i<M; i++)
{
int x, y;
scanf("%d %d", &x, &y);
add_edge(x, y);
}
for (int i=; i<N+; i++)
{
if (dfn[i]==-) tarjan(i);
}
int v=Component[][];
int num=Component[].size();
rdfs(v);
for (int i=; i<=N; i++)
{
if (!visited[i])
{
num=;
break;
}
}
printf("%d\n", num);
}
Popular Cows(POJ 2186)的更多相关文章
- (连通图 缩点 强联通分支)Popular Cows -- poj --2186
http://poj.org/problem?id=2186 Description Every cow's dream is to become the most popular cow in th ...
- Popular Cows POJ - 2186(强连通分量)
Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10, ...
- Popular Cows (POJ No.2186)
Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...
- poj - 2186 Popular Cows && poj - 2553 The Bottom of a Graph (强连通)
http://poj.org/problem?id=2186 给定n头牛,m个关系,每个关系a,b表示a认为b是受欢迎的,但是不代表b认为a是受欢迎的,关系之间还有传递性,假如a->b,b-&g ...
- Popular Cows(codevs 2186)
题意: 有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个关系,如(1,2)代表1欢迎2,关系可以传递,但是不可以相互,即1欢迎2不代表 ...
- poj 2186 Popular Cows (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- POJ 2186 Popular Cows (强联通)
id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 655 ...
- poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】
题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)
poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...
随机推荐
- [luogu4140] 奇数国
题目 在一片美丽的大陆上有100000个国家,记为1到100000.这里经济发达,有数不尽的账房,并且每个国家有一个银行.某大公司的领袖在这100000个银行开户时都存了3大洋,他惜财如命,因此会不时 ...
- 金题大战Vol.0 B、序列
金题大战Vol.0 B.序列 题目描述 给定两个长度为 \(n\) 的序列\(a\), \(b\). 你需要选择一个区间\([l,r]\),使得\(a_l+-+a_r>=0\)且\(b_l+-+ ...
- 【USACO13DEC】 最优挤奶 - 线段树
题目描述 FJ最近买了1个新仓库, 内含N 个挤奶机,1 到N 编号并排成一行. 挤奶机i 每天能产出M(i) 单位的奶.不幸的是, 机器装得太近以至于如果一台机器i 在某天被使用, 那与它相邻的两台 ...
- 【源码】RocketMQ如何实现获取指定消息
概要 消息查询是什么? 消息查询就是根据用户提供的msgId从MQ中取出该消息 RocketMQ如果有多个节点如何查询? 问题:RocketMQ分布式结构中,数据分散在各个节点,即便是同一Topic的 ...
- 性能分析(7)- 未利用系统缓存导致 I/O 缓慢案例
性能分析小案例系列,可以通过下面链接查看哦 https://www.cnblogs.com/poloyy/category/1814570.html 前提 前面有学到 Buffer 和 Cache 的 ...
- [Hei-Ocelot-Gateway ].Net Core Api网关Ocelot的开箱即用版本
写在前面 很多neter都有在用Ocelot做Api网关,但是Ocelot又不像kong或者其他网关一样,开箱即用.它需要你单独开一个web项目来部署,这样很多同学都在做重复的事了. 这里[Hei.O ...
- QT下载速度慢的解决方法
在官网的下载速度实在太慢了 找到了一个镜像网站 https://mirrors.tuna.tsinghua.edu.cn/qt/archive/qt/
- fatal error: glib.h: No such file or directory
在学习BLE bluez的时候,做了一个测试程序,看到gatttool.c下面有一个glib解析命令行的功能,想移植到自己的程序接口中,但是添加了#include <glib.h>后,出现 ...
- 七夕节表白3d相册制作(html5+css3)
七夕节表白3d相册制作 涉及知识点 定位 阴影 3d转换 动画 主要思路: 通过定位将所有照片叠在一起,在设置默认的样式以及照片的布局,最后通过设置盒子以及照片的旋转动画来达到效果. 代码如下: &l ...
- Rainbow: Combining Improvements in Deep Reinforcement Learning
郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! arXiv:1710.02298v1 [cs.AI] 6 Oct 2017 (AAAI 2018) Abstract 深度强化学习社区对D ...