图论:POJ2186-Popular Cows (求强连通分量)
Popular Cows
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 3
Sample Output
1
Hint
Cow 3 is the only cow of high popularity.
说实话这个强连通分量看的挺迷的,似懂非懂的(思想看懂了,代码的实现方法不太懂),暂时就不写解题心得了,等以后熟悉了再写。
#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1e5 + 100;
int dfn[maxn],low[maxn],head[maxn],degree[maxn],_stack[maxn],top,ans,num[maxn],Time;
bool vis[maxn];
struct node
{
int to,Next;
}edge[maxn];
void add_edge(int m)
{
int u,v;
int cnt = 0;
while(m--)
{
scanf("%d%d",&u,&v);
//注意存图的方法
edge[cnt].Next = head[u];
edge[cnt].to = v;
head[u] = cnt++;
}
}
void init()
{
//很烦的初始化但是要看清楚,edge和head是初始化为-1,这样可以便于分别
Time = 0;
ans = 0;
top = 0;
memset(dfn,0,sizeof(dfn));
memset(head,-1,sizeof(head));
memset(low,0,sizeof(low));
memset(edge,-1,sizeof(edge));
memset(vis,false,sizeof(vis));
memset(degree,0,sizeof(degree));
}
void tarjan(int u)
{
_stack[top] = u;
top++;
low[u] = dfn[u] = Time;
Time++;
vis[u] = true;
for(int i=head[u];i!=-1;i=edge[i].Next)
{
int v = edge[i].to;
if(!vis[v])
{
tarjan(v);
low[u] = min(low[u],low[v]);//找到这个点第一次在stack中出现的位置
}
else
low[u] = min(low[u],dfn[v]);
}
if(low[u] == dfn[u])//找到一个分量
{
ans++;//分量数目加一,从stack中弹出
while(top>0 && _stack[top] != u)
{
top--;
vis[_stack[top]] = true;
num[_stack[top]] = ans;
}
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
init();
add_edge(m);
for(int i=1;i<=n;i++)
if(!vis[i])
tarjan(i);
int sum = 0,x;
for(int i=1;i<=n;i++)
for(int j=head[i];j!=-1;j=edge[j].Next)
if(num[i] != num[edge[j].to])//计算缩点后每个点的出度
degree[num[i]]++;
for(int i=1;i<=ans;i++)
if(!degree[i])
{
sum++;
x = i;
}
int Ans = 0;
if(sum == 1)//只能形成一个缩点
{
for(int i=1;i<=n;i++)
if(num[i] == x)
Ans++;
printf("%d\n",Ans);
}
else
printf("0\n");
}
return 0;
}
图论:POJ2186-Popular Cows (求强连通分量)的更多相关文章
- POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23445 Accepted: 9605 Des ...
- POJ2186 Popular Cows 题解 强连通分量入门题
题目链接:http://poj.org/problem?id=2186 题目大意: 每头牛都想成为牛群中的红人. 给定N头牛的牛群和M个有序对(A, B),(A, B)表示牛A认为牛B是红人: 该关系 ...
- POJ2186 Popular Cows 题解 强连通分量
题目链接:http://poj.org/problem?id=2186 题目大意: 每头牛都想成为牛群中的红人. 给定N头牛的牛群和M个有序对(A, B),(A, B)表示牛A认为牛B是红人: 该关系 ...
- POJ2186 Popular Cows(强连通分量)
题目问一个有向图所有点都能达到的点有几个. 先把图的强连通分量缩点,形成一个DAG,那么DAG“尾巴”(出度0的点)所表示的强连通分量就是解,因为前面的部分都能到达尾巴,但如果有多个尾巴那解就是0了, ...
- poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】
题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- poj 2186 "Popular Cows"(强连通分量入门题)
传送门 参考资料: [1]:挑战程序设计竞赛 题意: 每头牛都想成为牛群中的红人. 给定N头牛的牛群和M个有序对(A, B),(A, B)表示牛A认为牛B是红人: 该关系具有传递性,所以如果牛A认为牛 ...
- POJ 2186 Popular Cows(强连通分量Kosaraju)
http://poj.org/problem?id=2186 题意: 一个有向图,求出点的个数(任意点可达). 思路: Kosaraju算法的第一次dfs是后序遍历,而第二次遍历时遍历它的反向图,从标 ...
- poj2186 Popular Cows(强连通)
崇拜有传递性.求所有牛都崇拜的牛tarjan算法求强连通. 如果不连通就不存在.如果联通,缩点后唯一一个出度为零的点就是答案,有多个则不存在. #include <vector> #inc ...
- POJ 2186 Popular Cows(强连通分量)
[题目链接] http://poj.org/problem?id=2186 [题目大意] 给出一张有向图,问能被所有点到达的点的数量 [题解] 我们发现能成为答案的,只有拓扑序最后的SCC中的所有点, ...
- [poj 2186]Popular Cows[Tarjan强连通分量]
题意: 有一群牛, a会认为b很帅, 且这种认为是传递的. 问有多少头牛被其他所有牛认为很帅~ 思路: 关键就是分析出缩点之后的有向树只能有一个叶子节点(出度为0). 做法就是Tarjan之后缩点统计 ...
随机推荐
- pycharm 虚拟环境virtualenv迁移到别的机器 无法读取包的问题
将virtualenv迁移到别的机器时,发现pycharm 总是无法读取目录下所在的包,后来经过实验终于找到了问题所在: 将自己所建的虚拟环境目录下的orig-prefix.txt中保存的路径,改成新 ...
- vue中的number
今天在vue文档中看到有number这样一个修饰符 觉得挺方便的就尝试了一下下面是代码 <body> <div id="box"> <input ty ...
- SQL Server插入中文数据出现乱码问题
我在用sql server存储数据的时候发现中文全变成了问号,我知道中文是特殊的编码.所以在数据库设计的时候包含中文的字段就是nvarchar,但是还是成了问号 好了,不多说了,解决方案如下: 在存储 ...
- JS中数组的介绍
一.数组: 一组数据的集合: 二.JS中数组的特点: 1.数组定义时无需指定数据类型: 2.数组定义时可以无需指定数组长度: 3.数组可以存储任何类型的数据: 4.一般是相同的数据类型: 三.数组的创 ...
- Spring AOP初步总结(二)
该篇为Spring AOP的一个应用案例:系统日志 需求:将任何删除,更改或新增数据库的操作汇总到数据库中 步骤1:编写切面 @Aspect @Component public class SysLo ...
- top 进程管理
top 动态查看进程 前五行解释: 第一行参数说明: top - 07:06:19 当前时间 up 10 min, 系统运行时间,格式为时:分 1 user, 当前登录用户数 load av ...
- 浅析ES6中的iterator
1.iterator迭代器必须保证其遍历终止条件可控,否则会形成死循环demo: //会用到iterator接口的场合 //1.for...of循环 //2. ...解构表达式 const obj = ...
- 前端开发神器 - Brackets
做了几年的 .Net 项目开发,后来公司转 Java 语言开发,Java 做了还没一年,公司准备前后端分离开发,而我被分到前端! Brackets是一款基于web(html+css+js)开发的web ...
- sysdig安装和使用介绍
安装步骤1)安装资源库rpm --import https://s3.amazonaws.com/download.draios.com/DRAIOS-GPG-KEY.publiccurl -s -o ...
- UVA821 PageHopping (Floyd)
求所有点直接的平均最短距离,保存一下出现过的点,题目保证是所有点连通,Floyd求出最短路以后两个for统计一下. #include<bits/stdc++.h> using namesp ...