图论: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之后缩点统计 ...
随机推荐
- Windows下打开某些软件时显示显卡驱动不是最新的问题
在Windows下打开某些对显卡要求比较高的软件时,会出现某些显卡驱动不是最新,要求更新到最新的提示,但是当你真的去更新显卡驱动的时候,却发现现在的显卡驱动已经是最新了,那么为什么还会有这样的提示呢, ...
- 玲珑杯”ACM比赛 Round #4 1054 - String cut 暴力。学到了扫描的另一种思想
http://www.ifrog.cc/acm/problem/1054 问删除一个字符后的最小循环节是多少. 比赛的时候想不出,不知道怎么暴力. 赛后看了别人代码才晓得.唉,还以为自己字符串还不错, ...
- HDU 5230 ZCC loves hacking 大数字的整数划分
http://acm.hdu.edu.cn/showproblem.php?pid=5230 把题目简化后,就是求 1---n - 1这些数字中,将其进行整数划分,其中整数划分中不能有重复的数字,如果 ...
- IIS+PHP访问量大时内存爆满等性能问题解决方案
如今还是有许多人在用老掉牙的 IIS6 + PHP.本文解决方法适用于使用 FastCGI 运行 PHP 的用户. 问题原因: 你可以试一试,你在 VPS 上用 IIS6 安装 FastCGI 跑 P ...
- codeforces736D. Permutations(线性代数)
题意 $m \leqslant 500000$,题目打错了 Sol 神仙题Orz 构造矩阵$B$,使得$B[b[i]][a[i]] = 1$ 那么他的行列式的奇偶性也就对应了生成排列数列数量的奇偶性( ...
- Open edX 配置 O365 SMTP
配置LMS/Studio SMTP: 用到的文件如下:以下设置采用的root用户进行 /edx/app/edxapp/lms.env.json #|env文件 里包含一些功能开关 /edx/app/e ...
- mysql> set sql_mode='no_auto_value_on_zero';
mysql> set sql_mode='no_auto_value_on_zero';
- java.lang.IllegalArgumentException: name MUST NOT NULL! at org.nutz.dao.impl.NutDao.fetch
Nutz传值报错问题 作者:Vashon 时间:20150902 平台:Nutz框架 Java后台方法中拿值时报的错 报错信息: java.lang.IllegalArgumentException: ...
- PHP小数处理常用函数
1.php保留两位小数并且四舍五入 $num = 123213.666666; echo sprintf("%.2f", $num); // 123213.67echo round ...
- sysbench0.5安装和使用介绍
sysbench是一个模块化的.跨平台.多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况,sysbench支持MySQL.PostgreSQL.Oracle数据库OLTP测试.它 ...