图论: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之后缩点统计 ...
随机推荐
- Codeforces Round #542(Div. 2) A.Be Positive
链接:https://codeforces.com/contest/1130/problem/A 题意: 给n个数,找出一个非0整数d,使所有n个数除以整数d后,数组中正数的数量>= n/2. ...
- Codeforces 1132E(转化+dp)
要点 假设第i个最后总共选的值为ci,不妨把它分成两部分:\[c_i=cnt'_i*L+q_i\]\[L=840,\ 0<=q_i<L\]又可以写成:\[c_i=cnt_1*i+cnt_2 ...
- JavaScript特点、优缺点及常用框架
参考来源: http://www.cnblogs.com/SanMaoSpace/archive/2013/06/14/3136774.html
- F. Bakkar In The Army 二分
http://codeforces.com/gym/100283/problem/F 思路是二分第几行,二分出来的行是总和 >= n的,那么第k - 1行一定要选,那么再在第k行中二分那一列. ...
- 一款被嫌弃的字体「Comic Sans」
这是我在其他blog上看到的字体,看到的第一眼就觉得它很有意思,但并不知道它的来历.后面google了一番,这字体叫Comic Sans,背后有不少有趣的轶事,下面贴一篇介绍它的文章. 以下内容转载自 ...
- jmeter压力测试中遇到的问题汇总
1.线程数大于1的时候,计数器配置没有勾选reset counter选项,导致测试结果出错 正常结果: 实际结果:index大于count数量时出错,病区及床号直接显示在count的基础上开始加1了 ...
- Asp.net开发必备51种代码
1.//弹出对话框.点击转向指定页面 Response.Write("<script>window.alert('该会员没有提交申请,请重新提交!')</script> ...
- Android 滑动RecyclerView时隐藏部分控件
在使用RecyclerView控件时,上下拖动控件时的时候,需要实时的隐藏与显示部分控件,已到达很好的用户体验. 原理很简单,当RecyclerView拖动至最上层时显示控件,当RecyclerV ...
- PL/SQL学习笔记(四)之——删除重复记录
例:假设员工表中有若干记录重复,请删除重复的记录(某企业面试题) ------模拟建表 create table employee( e_id varchar2(20) primary key, e_ ...
- 浅析linux下软件的安装
Linux环境: CentOs 6.0 知识点介绍: 一.tarball安装 安装步骤: 将tarball文件在/usr/local/src目录解压缩 ./configure:这个步骤是建立makef ...