Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 35644   Accepted: 14532

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
题目大意:
  给定一个有向图,求有多少个顶点是由任何顶 点出发都可达的。
 
题解:
  Tarjan缩点构成DAG
  若DAG上面如果有唯一的出度为0的点,则该点能被所有的点可达。那么该点所代表的连通分量上的所有的原图中的点,都能被原图中的所有点可达,则该连通分量的点数,就是答案。 
  若DAG上面如果有不止一个出度为0的点,则这些点互相不可达,原问题无解,答案为0
 
代码:
//by 减维
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<algorithm>
#define ll long long
#define maxn
using namespace std; struct edge{
int fr,to,ne;
}e[]; int n,m,ecnt,cnt,num,tot,ans,ans2;
int map[],dfn[],low[],chu[],shu[];
int head[],zhan[];
bool pd[]; void add(int x,int y)
{
e[++ecnt].to=y;
e[ecnt].fr=x;
e[ecnt].ne=head[x];
head[x]=ecnt;
} void tarjan(int x)
{
dfn[x]=low[x]=++num;
zhan[++tot]=x;
pd[x]=;
for(int i=head[x];i;i=e[i].ne)
{
int dd=e[i].to;
if(!dfn[dd]){
tarjan(dd);
low[x]=min(low[x],low[dd]);
}else if(pd[dd]){
low[x]=min(dfn[dd],low[x]);
}
}
if(dfn[x]==low[x]){
cnt++;
int t;
do{
t=zhan[tot--];
map[t]=cnt;
pd[t]=;
shu[cnt]++;
}while(t!=x);
}
} void init()
{
cnt=ecnt=num=tot=ans=;
memset(e,,sizeof(e));
memset(pd,,sizeof(pd));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(map,,sizeof(map));
memset(chu,,sizeof(chu));
memset(shu,,sizeof(shu));
memset(head,,sizeof(head));
memset(zhan,,sizeof(zhan));
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i=;i<=m;++i){
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
}
for(int i=;i<=n;++i)if(!dfn[i])tarjan(i);
//for(int i=1;i<=n;++i)printf("%d ",dfn[i]);printf("\n");
//for(int i=1;i<=n;++i)printf("%d ",low[i]);printf("\n");
//for(int i=1;i<=n;++i)printf("%d ",map[i]);printf("\n");
for(int i=;i<=ecnt;++i)
{
int x=e[i].fr,dd=e[i].to;
if(map[x]!=map[dd])chu[map[x]]++;
}
memset(pd,,sizeof(pd));
for(int i=;i<=n;++i)
if(chu[map[i]]==&&!pd[map[i]])
pd[map[i]]=,ans++,ans2=shu[map[i]];
if(ans==)printf("%d\n",ans2);
else printf("0\n");
}
}

【Tarjan缩点】POJ2186 Popular Cows的更多相关文章

  1. 强连通分量tarjan缩点——POJ2186 Popular Cows

    这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定 ...

  2. 洛谷——P2341 [HAOI2006]受欢迎的牛//POJ2186:Popular Cows

    P2341 [HAOI2006]受欢迎的牛/POJ2186:Popular Cows 题目背景 本题测试数据已修复. 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所 ...

  3. POJ-2186 Popular Cows,tarjan缩点找出度为0的点。

    Popular Cows 题意:一只牛崇拜另外一只牛,这种崇拜关系可以传导.A->B,B->C =>A->C.现在给出所有的关系问你有多少牛被其他所有的牛都崇拜. 思路:就是一 ...

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

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

  5. POJ2186 Popular Cows [强连通分量|缩点]

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31241   Accepted: 12691 De ...

  6. POJ2186 Popular Cows

    Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...

  7. poj2186 Popular Cows 题解——S.B.S.

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29642   Accepted: 11996 De ...

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

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

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

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

随机推荐

  1. &quot;Insufficient RAM for Flash Algorithms&quot;出错原因及解决方式

    "Insufficient RAM for Flash Algorithms"错误通常会有一个"cannot load flash programming algorit ...

  2. Simple prefix compression

    题目 看懂题目的意思 直接模拟就能够了 好像不用递归就能够了. . 尽管这周学的是递归 还是偷了一些懒  直接模拟 在说这个题目的意思 本来能够写的非常清楚的下标 题目非要把两个字符串的表示方法写的这 ...

  3. PHP csv文件内容转成数组/Json

    $lines = array_map('str_getcsv', file($filePath));; $result = array(); $headers = null; if (count($l ...

  4. win64环境下使用curl命令

    想在windows环境下使用curl命令,其实很简单,简单配置如下: 工具下载 在官网下载工具包:https://curl.haxx.se/download.html 我这里下载的是zip版本的,下载 ...

  5. 《大型网站技术架构:核心原理与案例分析》【PDF】下载

    <大型网站技术架构:核心原理与案例分析>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230062557 内容简介 本书通过梳理大型网站 ...

  6. SSH连接工具:SecureCRT设置,另一个SSH连接工具:Xshell。在Windows和Linux之间互传文件可用WinSCP

    一般Linux发行版不允许root远程登录,CentOS允许. 调整字体大小:

  7. Node: 如何控制子进程的输出

    大家知道,在一个node程序中,如果当前进程想要生成一个子进程,它可以调用child_process模块的spawn方法.spawn方法签名如下: child_process.spawn(comman ...

  8. java获取当前应用的运行信息(内存,线程,运行时间,状态等)

    一:目的 写这一段程序的原因是需要监控部署的的应用是否正常运行,并且显示其运行状态.在进程莫名死掉后甚至可以自动启动该应用. 首先这段代码可以获取的信息如下 /** * 当前进程运行的主机名 */ p ...

  9. js获取图片的EXIF,解决图片旋转问题

    相信大家在做项目的时候会遇到在canvas里加入图片时,图片发生90°,180°的旋转.当时的你肯定时懵逼的,为毛. 其实这就是图片的EXIF搞的鬼. 什么是EXIF 简单来说,Exif 信息就是由数 ...

  10. ArcGIS API for JavaScript 4.2学习笔记[31] (补充学习)Task类

    Task这个东西很有用,是AJS中用于解决各种乱七八糟任务的一个类.它有很多子类,有用于空间分析的,有用于空间查询的,等等. 这篇作为补充学习的第一篇,也是进阶学习的第一篇,我就改个写法. 我将使用思 ...