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. 0-systemctl开机启动项

    防火墙:iptables Apache服务名称:httpd MySQL服务名称:mysqld VSFTP服务名称:vsftpd <!--CentOS7新指令--> 使某服务 自动启动 sy ...

  2. html5,新增的元素,fieldset legend

    <form action="">    <fieldset>        <legend>用户注册</legend>       ...

  3. nodejs框架express快速开始

    认识express 创建应用 get请求 简述中间件 all方法 use方法1 use方法2 回调函数 获取主机.路径名 Get请求 - query Get请求 - param Get请求 - par ...

  4. HTML5 UI框架Kendo UI Web中如何实现Grid网格控件本地化

    Kendo UI Web包含数百个创建HTML5 web app的必备元素,包括UI组件.数据源.验证.一个MVVM框架.主题.模板等. 为了使得产品可以符合不同市场的本地化需求和语言,Kendo U ...

  5. python核心编程学习记录之模块

  6. SQL标签

    SQL标签库提供了与关系型数据库进行交互的标签. 引入语法:<%@ taglib prefix="sql" uri="http://java.sun.com/jsp ...

  7. 使用C# yield关键字来提高性能和可读性

    对于”yield”这个关键字我已经见过N次了,直到最近我才知道这个关键字所蕴含的力量.我将在下面展示出一些使用”yield”让你的代码有更高可读性和更好性能的例子. 为了让你对yield有一些快速概览 ...

  8. php socket的一些问题

    在php手册看到了php socket的例子 但有些socket_read的循环判断有一些问题 造成进程的阻塞 实例是用phpsocket实现 客户端连接到socket server 发送文本 接受文 ...

  9. python:页面布局 后台管理页面之常用布局

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  10. 关于NSLog

    #ifdef __OBJC__#ifdef DEBUG#define NSLog(fmt,...) NSlog((@"%s [Line %d]" fmt),__PRETTY_FUN ...