POJ 2186-Popular Cows (图论-强联通分量Korasaju算法)
题目链接:http://poj.org/problem?id=2186
题目大意:有n头牛和m对关系, 每一对关系有两个数(a, b)代表a牛认为b牛是“受欢迎”的,且这种关系具有传递性, 如果a牛认为b牛“受欢迎”, b牛认为c牛“受欢迎”, 那么a牛也认为c牛“受欢迎”。 现在想知道有多少头牛受除他本身外其他所有牛的欢迎?
解题思路:如果有两头或者多头牛受除他本身外其他所有牛的欢迎, 那么在这两头或者多头牛之中, 任意一头牛也受两头或者多头牛中别的牛的欢迎, 即这两头或者多头牛同属于一个强联通分量, 且其他强联通分量都可以到达该强联通分量。那么可以用Korasaju算法进行强联通分量的分解, 然后还能够得到各个强联通分量拓扑排序后的顺序, 那么唯一可以成为解的只有拓扑序最后的那个联通分量, 并且需要检查其他强联通分量是否能全部到达这个强联通分量, 如果能够全部可达, 那么该强联通分量有多少元素即为问题的解, 否则为0;
强联通分量的分解的步骤:
1:对于图G, 深度优先遍历G, 算出每个节点u结束的时间s[u], 起点如何选择无所谓。
2:对于图G的转置图rG, 选择遍历的起点时, 按照节点的结束时间从大到小进行, 遍历过程中, 一遍遍历, 一遍给节点做分类标记, 每找到一个新的起点, 分类标记加一
3:对于第二步骤中,每一个相同标记的点即为一个强联通分量
代码如下:
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
const int N = ; vector<int>G[N], rG[N], vs;
bool used[N];
int v;
int cmp[N]; void add_edge(int a, int b)
{
G[a].push_back(b);
rG[b].push_back(a);
} void dfs(int n)
{
used[n] = true;
for(int i=; i<G[n].size(); ++ i)
{
int v = G[n][i];
if(used[v] == false)
dfs(v);
}
vs.push_back(n);
} void rdfs(int n, int k)
{
used[n] = true, cmp[n] = k;
for(int i=; i<rG[n].size(); ++ i)
{
int v=rG[n][i];
if(used[v] == false)
rdfs(v, k);
}
} int scc()
{
memset(used, false, sizeof(used));
vs.clear();
for(int i=; i<v; ++ i)
if(used[i] == false)
dfs(i); memset(used, false, sizeof(used));
int k=;
for(int i=vs.size()-; i>=; -- i)
{
if(used[vs[i]] == false)
rdfs(vs[i], k++);
}
return k;
} int main()
{
int m;
scanf("%d%d", &v, &m);
for(int i=; i<=m; ++ i)
{
int a, b;
scanf("%d%d", &a, &b);
add_edge(a-, b-);
} int n = scc(); int u = , num = ;
for(int i=; i<v; ++ i)
if(cmp[i] == n-)
{
u = i, num ++;
}
memset(used, false, sizeof(used));
rdfs(u, );
for(int i=; i<v; ++ i)
if(used[i] == false)
{
num = ;
break;
}
printf("%d\n", num);
}
注:新学的强联通分量,那里不对请指出, 万分感谢!
POJ 2186-Popular Cows (图论-强联通分量Korasaju算法)的更多相关文章
- POJ 2186 Popular cows(Kosaraju+强联通分量模板)
题目链接:http://poj.org/problem?id=2186 题目大意:给定N头牛和M个有序对(A,B),(A,B)表示A牛认为B牛是红人,该关系具有传递性,如果牛A认为牛B是红人,牛B认为 ...
- POJ 2186 Popular Cows(强联通+缩点)
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(强连通分量缩点)
题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...
- 强连通分量分解 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(强联通分量)
题目链接:http://poj.org/problem?id=2186 题目大意: 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种 ...
- POJ 2186 Popular Cows (强联通)
id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 655 ...
- poj 2186 Popular Cows (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- [强连通分量] POJ 2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31815 Accepted: 12927 De ...
- poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】
题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
随机推荐
- 常见C内存管理程序
本文主要关注的是C内存管理程序,比较著名的几个C内存管理程序,其中包括: l Doug Lea Malloc:Doug Lea Malloc实际上是完整的一组分配程序,其中包括Doug Lea的原 ...
- mac 下更新 .bash_profile 文件
1.打开terminal(终端) 2.cd ~ ( 进入当前用户的home目录) 3.open .bash_profile (打开.bash_profile文件,如果文件不存在就 创建文件:touc ...
- GridView使用自带分页功能时分页方式及样式PagerStyle
// 转向地址:http://www.bubuko.com/infodetail-412562.html GridView分页,使用自带分页功能,类似下面样式: 在aspx页面中,GridView上的 ...
- 【python】传入函数
def add(x, y, f): return f(x) + f(y) 当我们调用add(-5, 6, abs)时,参数x,y和f分别接收-5,6和abs,根据函数定义,我们可以推导计算过程为: x ...
- SQL:查找被锁的表,以及锁表的SQL语句(重点推荐)
--死锁检测 use master Select * --找到SPID exec sp_lock --根据SPID找到OBJID ) --根据OBJID找到表名 1.DatabaseName 同于你要 ...
- 服务发现之 Etcd VS Consul
抄自这里 *********************************************************************************************** ...
- <<人性的弱点>>读书笔记
书名的英文名其实是<< How to win friends and influence people & how to stop worrying and start livin ...
- ruby中rsa加签解签方法
# coding:utf-8require 'openssl'require 'base64'# rsa签名,文本内容和私钥路径def rsa_sign(data,private_key_path) ...
- Code Igniter + PHP5.3 + SqlServer2008配置
1.配置apache+php5.3 2.配置sql server服务器,并允许远程连接. 3.去http://www.microsoft.com/en-us/download/details.aspx ...
- 自动improt的xcode插件 Auto-Importer
https://github.com/lucholaf/Auto-Importer-for-Xcode