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 (求强连通分量)的更多相关文章

  1. POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23445   Accepted: 9605 Des ...

  2. POJ2186 Popular Cows 题解 强连通分量入门题

    题目链接:http://poj.org/problem?id=2186 题目大意: 每头牛都想成为牛群中的红人. 给定N头牛的牛群和M个有序对(A, B),(A, B)表示牛A认为牛B是红人: 该关系 ...

  3. POJ2186 Popular Cows 题解 强连通分量

    题目链接:http://poj.org/problem?id=2186 题目大意: 每头牛都想成为牛群中的红人. 给定N头牛的牛群和M个有序对(A, B),(A, B)表示牛A认为牛B是红人: 该关系 ...

  4. POJ2186 Popular Cows(强连通分量)

    题目问一个有向图所有点都能达到的点有几个. 先把图的强连通分量缩点,形成一个DAG,那么DAG“尾巴”(出度0的点)所表示的强连通分量就是解,因为前面的部分都能到达尾巴,但如果有多个尾巴那解就是0了, ...

  5. poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】

    题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  6. poj 2186 "Popular Cows"(强连通分量入门题)

    传送门 参考资料: [1]:挑战程序设计竞赛 题意: 每头牛都想成为牛群中的红人. 给定N头牛的牛群和M个有序对(A, B),(A, B)表示牛A认为牛B是红人: 该关系具有传递性,所以如果牛A认为牛 ...

  7. POJ 2186 Popular Cows(强连通分量Kosaraju)

    http://poj.org/problem?id=2186 题意: 一个有向图,求出点的个数(任意点可达). 思路: Kosaraju算法的第一次dfs是后序遍历,而第二次遍历时遍历它的反向图,从标 ...

  8. poj2186 Popular Cows(强连通)

    崇拜有传递性.求所有牛都崇拜的牛tarjan算法求强连通. 如果不连通就不存在.如果联通,缩点后唯一一个出度为零的点就是答案,有多个则不存在. #include <vector> #inc ...

  9. POJ 2186 Popular Cows(强连通分量)

    [题目链接] http://poj.org/problem?id=2186 [题目大意] 给出一张有向图,问能被所有点到达的点的数量 [题解] 我们发现能成为答案的,只有拓扑序最后的SCC中的所有点, ...

  10. [poj 2186]Popular Cows[Tarjan强连通分量]

    题意: 有一群牛, a会认为b很帅, 且这种认为是传递的. 问有多少头牛被其他所有牛认为很帅~ 思路: 关键就是分析出缩点之后的有向树只能有一个叶子节点(出度为0). 做法就是Tarjan之后缩点统计 ...

随机推荐

  1. getAnnotation为null的坑

    在写一个基于SpringAOP的权限控制的. 自己定义了一个注解,然后逻辑代码需要通过获取自定义注解的一个属性来进行权限控制. 下面简单上一下关键代码: 自定义注解: @Documented //有关 ...

  2. D. Kay and Snowflake 树的重心

    http://codeforces.com/contest/686/problem/D 给出q个询问,每次要求询问以x为根的子树中,哪一个点是重心. 树的重心:求以cur为根的子树的重心,就是要找一个 ...

  3. kafka系列一:单节点伪分布式集群搭建

    Kafka集群搭建分为单节点的伪分布式集群和多节点的分布式集群两种,首先来看一下单节点伪分布式集群安装.单节点伪分布式集群是指集群由一台ZooKeeper服务器和一台Kafka broker服务器组成 ...

  4. kafka基础四

    消费者消费过程(二) 消费组状态机:消息的产生存储消费看似是杂乱无章的,但万物都会遵循一定的规则成长,任何事物的发展都是有迹可循的. 开始消费组初始状态为Stable,经过第一次Rebalance之后 ...

  5. Ubuntu系统下安装字体和切换默认字体的方法

    参考链接:http://my.oschina.net/itblog/blog/278566 打开Ubuntu的软件中心,搜索:tweak,安装[Unity Tweak Tool]这款软件,如图(由于我 ...

  6. vue2.0:(四)、首页入门,组件拆分1

    为什么需要组件拆分呢?这样才能更符合模块化这样一个理念. 首先是index.html,代码如下: <!DOCTYPE html> <html> <head> < ...

  7. JS常用的技术

    思考与总结 1.模块化 曾看到某大牛说:模块化和组件化是前端开发的一大趋势.所谓的模块化一般是指为了实现一个特定的功能而将所有的代码(对象)封装成一个模块.而AMD就是requireJS为指定模块规范 ...

  8. IE浏览器兼容background-size

    background-size是CSS3新增的属性,IE8以下不支持,通过滤镜实现background-size效果 background-size:contain; // 缩小图片来适应元素的尺寸( ...

  9. JavaScript数据格式验证探讨

    1.需求 修改某个文本框数据,要求对修改后的格式做验证(必须是数字). 注:实际需求比上述复杂,为了说明问题,这里特意简化了需求(如:对修改后数据依赖条件的判断,数据入库等). 2.关于NaN的探讨( ...

  10. MFC U盘检测

    WM_DEVICECHANGE消息 查阅MSDN得知: The framework calls this member function to notify an application or dev ...