Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 31815   Accepted: 12927

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. 

Source

原题大意:给n头牛,m对形式(A,B)代表A觉得B很厉害,特殊的,如果A觉得B很厉害,B觉得C很厉害,那么A觉得C也很厉害,问有多少头牛被所有牛(除了它自己)觉得很厉害。
 
本题比较考验基础,涉及到链表,强连通分量的有关知识,对初学图论者还是有好处的。
 
解题思路:设n头牛中两两之间都觉得对方很厉害为一堆牛,而其中有一头牛被所有牛都认为很厉害,那么很显然,这堆牛都被所有牛认为很厉害。
              那会不会有两堆以上的牛都被所有人认为很厉害呢?显然不会,如果两堆牛都被所有人认为很厉害,那么这两堆牛两两之间一定都觉得自己很厉害,其实还是一堆牛。
              那么,这就可以演化为一个图,有n个点,m条有向边,求唯一的一个出度为0的强联通分量中元素的个数。
              为什么唯一呢?
              显然,如果有两个出度为0的强连通分量,那么这两个强连通分量都不会觉得对方分量的牛很厉害,而原题是找被所有牛都觉得很厉害,于是此时没有牛被所有牛认为很厉害。
             我们用链表存储点的关系,用tarjian找强连通分量,最后用强连通分量的出度判断是否是我们找的那一群牛,统计牛的个数就可以了。
上代码:

#include<stdio.h>
#include<string.h>
struct list
{
int v;
list *next;
};
list *head[10010],*rear[10010];
int dfn[10010],low[10010],stack[10010],s[10010],top,times,cnt;
bool instack[10010],chu[10010];
void tarjian(int v)
{
dfn[v]=low[v]=++times;
instack[v]=true;
stack[top++]=v;
for(list *p=head[v];p!=NULL;p=p->next)
if(!dfn[p->v])
{
tarjian(p->v);
if(low[p->v]<low[v]) low[v]=low[p->v];
}else if(instack[p->v]&&low[p->v]<low[v]) low[v]=low[p->v];
if(low[v]==dfn[v])
{
++cnt;
do
{
v=stack[--top];
instack[v]=false;
s[v]=cnt;
}while(low[v]!=dfn[v]);
}
return;
}
int main()
{
int n,m,i,begin,to,a,b;
scanf("%d%d",&n,&m);
memset(rear,0,sizeof(rear));
memset(head,0,sizeof(head));
for(i=0;i<m;++i)
{
scanf("%d%d",&begin,&to);
if(rear[begin]!=NULL)
{
rear[begin]->next=new list;
rear[begin]=rear[begin]->next;
}else head[begin]=rear[begin]=new list;
rear[begin]->next=NULL;
rear[begin]->v=to;
}
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(stack,0,sizeof(stack));
memset(s,0,sizeof(s));
times=cnt=top=0;
for(i=1;i<=n;++i) if(!dfn[i]) tarjian(i);
if(cnt==1)
{
printf("%d\n",n);
return 0;
}
memset(chu,false,sizeof(chu));
for(i=1;i<=n;++i)
for(list *p=head[i];p!=NULL;p=p->next)
if(s[i]!=s[p->v])
chu[s[i]]=true;
a=0;
for(i=1;i<=cnt;++i) if(chu[i]) ++a;else b=i;
if(a==cnt-1)
{
a=0;
for(i=1;i<=n;++i) if(s[i]==b) ++a;
printf("%d\n",a);
}else printf("0\n");
return 0;
}

  

   

[强连通分量] POJ 2186 Popular Cows的更多相关文章

  1. 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)

    poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...

  2. poj 2186 Popular Cows (强连通分量+缩点)

    http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

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

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

  4. POJ 2186 Popular Cows (强联通)

    id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 655 ...

  5. tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows

    缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...

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

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

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

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

  8. POJ 2186 Popular Cows --强连通分量

    题意:给定一个有向图,问有多少个点由任意顶点出发都能达到. 分析:首先,在一个有向无环图中,能被所有点达到点,出度一定是0. 先求出所有的强连通分支,然后把每个强连通分支收缩成一个点,重新建图,这样, ...

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

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

随机推荐

  1. SVN和Git的异同

    其实Git和SVN还是挺像的,都有提交,合并等操作,看来这是源码管理工具的基本操作. 1. Git是分布式的,SVN是集中式的,好处是跟其他同事不会有太多的冲突,自己写的代码放在自己电脑上,一段时间后 ...

  2. Case1:WorkFlow不能运行的解决办法

    原因为CRMAppPool选择了一个域用户,然后异步服务的用户执行会有问题 at CrmException..ctor(Int32 errorCode, Object[] arguments) ilO ...

  3. VS2010调试Qt5的相关设置

    1.windows环境,下载离线安装包安装: 2.安装Qt5 Visual Studio Add-in并安装: 3.环境变量里设置QTDIR=D:\LIB\Qt\Qt5.3.2\5.3\msvc201 ...

  4. python核心编程学习记录之文件和输入输出

  5. 让DIV水平和垂直居中的几种方法

    我们在设计页面的时候,经常要把DIV居中显示,而且是相对页面窗口水平和垂直方向居中显示,如让登录窗口居中显示.我们传统解决的办法是用纯CSS来让DIV居中.在本文中,我将给大家讲述如何用CSS和jQu ...

  6. [转] ubuntu开启SSH服务

    点击阅读原文 SSH分客户端openssh-client和openssh-server如果你只是想登陆别的机器的SSH只需要安装openssh-client(ubuntu有默认安装,如果没有则sudo ...

  7. [算法]检测空间三角形相交算法(Devillers & Guigue算法)

    #pragma once //GYDevillersTriangle.h /* 快速检测空间三角形相交算法的代码实现(Devillers & Guigue算法) 博客原地址:http://bl ...

  8. 浅谈文本溢出省略号代表修剪text-overflow

    一.示例 图片显示: HTML结构: CSS样式: 注意: CSS3 text-overflow 属性规定当文本溢出包含元素时发生的事情,其中 所有浏览器都支持 white-space 属性.  示例 ...

  9. Flowplayer-playlist

    SOURCE URL: https://flowplayer.org/docs/playlist.html HTML layout Here is a typical setup for a play ...

  10. CentOS7 基础配置

    Centos 7 部分>>>>>>>>>>>>>>>>>>>>>>& ...