Popular Cows (POJ No.2186)
Description
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
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
Output
Sample Input
3 3
1 2
2 1
2 3
Sample Output
1 题解:
考虑以牛为顶点的有向图,对每个需对(A,B)连一条从A到B的边。我们不妨假设两头牛A,B都被其他牛认为是红牛。那么就知道A,B一定同属一个强连通分量,即存在一个包含A,B两个顶点的圈。反之,如果一个牛被其他牛认为是红牛,那么他所属的强连通分量中的牛一定全部是红牛。所以我们只需要找出拓扑序最大的强连通分量的个数就可以了。 AC代码:
#include<iostream>
#include<cctype>
using namespace std;
const int MAXN=+;
//-------------------------
void read(int &x){
x=;char ch=getchar();int f=;
for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';
x*=f;
}
//-------------------------
int n,m,tmp;
int topo[MAXN],cmp[MAXN];
bool vis[MAXN];
int first[MAXN],next[MAXN],v[MAXN],e;
void AddEdge(int a,int b){
v[++e]=b;
next[e]=first[a];
first[a]=e;
} int rfirst[MAXN],rnext[MAXN],rv[MAXN],re;
void rAddEdge(int a,int b){
rv[++re]=b;
rnext[re]=rfirst[a];
rfirst[a]=re;
}
//-------------------------
void dfs(int x){
vis[x]=;
for(int i=first[x];i;i=next[i])
if(!vis[v[i]])dfs(v[i]);
topo[++tmp]=x;
} void rdfs(int x,int k){
vis[x]=;
cmp[x]=k;
for(int i=rfirst[x];i;i=rnext[i])
if(!vis[rv[i]])rdfs(rv[i],k);
}
//---------------------------
int k=;
int scc(){
memset(vis,,sizeof(vis));
memset(topo,,sizeof(topo));
for(int i=;i<=n;i++){
if(!vis[i])dfs(i);
}
memset(vis,,sizeof(vis));
for(int i=n;i>=;i--)if(!vis[topo[i]])rdfs(topo[i],k++);
return k-;
}
//---------------------------
int main(){
read(n);read(m);
for(int i=;i<=m;i++){
int x,y;
read(x);read(y);
AddEdge(x,y);
rAddEdge(y,x);
}
int nn=scc(); int u=,num=;
for(int i=;i<=n;i++)
if(cmp[i]==nn){u=i;num++;}
memset(vis,,sizeof(vis));
rdfs(u,);
for(int i=;i<=n;i++)
if(!vis[i]){
num=;
break;
}
printf("%d\n",num);
}
Popular Cows (POJ No.2186)的更多相关文章
- Popular Cows(POJ 2186)
原题如下: Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 40746 Accepted: 16 ...
- (连通图 缩点 强联通分支)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, ...
- 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 ...
- 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. 求 ...
- poj 2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29908 Accepted: 12131 De ...
随机推荐
- WDCP一些常用的一健安装包可选安装组件
为有更好的性能,也为更简洁的系统,一些不是常用或不是基本的功能,都将做为可选的安装组件需要用到的就安装 memcache的安装 wget -c http://down.wdlinux.cn/in/me ...
- getAttribute:取得属性; setAttribute:设置属性。
input.setAttribute("title"); 当它是一个值,就是取值. input.setAttribute("title", "hell ...
- java中ExecutorService接口
一.声明 public interface ExecutorService extends Executor 位于java.util.concurrent包下 所有超级接口:Executor 所有已知 ...
- Deep Residual Learning for Image Recognition(MSRA-深度残差学习)
转自:http://blog.csdn.net/solomonlangrui/article/details/52455638 ABSTRACT: 神经网络的训练因其层次加深而 ...
- reaver 使用方法和技巧
reaver非常的不错,为我们ceng网带了最大的方便,使用简单,我来讲一下自己使用心得吧! 第一步,如果用虚拟机用vmware的,总会出现鼠标不灵点不到地方,换了一个 6.0.2 build-598 ...
- 转:fopen()函数
1.2 文件的输入输出函数 键盘.显示器.打印机.磁盘驱动器等逻辑设备, 其输入输出都可以通过文件管理的方法来完成.而在编程时使用最多的要算是磁盘文件, 因此本节主要以磁盘文件为主, 详细介绍Turb ...
- shell脚本实例一,移动文件夹中大于2000B的文件到另一个文件夹
shell脚本能帮我们简化linux下的一些工作,现在有个需求,把TMPA文件夹下大于2000B的文件都移动到TMPB下 #! /bin/bash function movefiles() { ` d ...
- 使用MapReduce将HDFS数据导入到HBase(二)
package com.bank.service; import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf. ...
- JS(五)
感觉JS里面还是有很多小技巧的,知道套路了,其实实现起来其实也还没有想象中的那么复杂.不过我觉得还是要把所学的知识融会贯通吧,不能学了JS就忘了前面的知识,结合起来才会威力无穷. 1.跑马灯:弹弹弹 ...
- android源代码百度网盘分享
转载请标明出处: http://blog.csdn.net/yujun411522/article/details/46334123 本文出自:[yujun411522的博客] 近期在使用Ubunt ...