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. ListView原理

    表明转载自http://blog.csdn.net/iispring/article/details/50967445 在自己定义Adapter时,我们经常会重写Adapter的getView方法,该 ...

  2. android.app.Activity 的介绍

    发现当前Android的资料不是非常多,并且对于Activity的介绍也非常少.所以把官方文档的android.app.Activity的介绍翻译了一下,增加了一些自己的理解.各位假设认为我自己理解的 ...

  3. Vue深度学习(2)

    Text 可以在表单的input 元素上使用v-model 指令来创建双向数据绑定.它会根据input元素的类型自动选取正确的绑定模式. <div id="app"> ...

  4. Python绘制3d螺旋曲线图实例代码

    Axes3D.plot(xs, ys, *args, **kwargs) 绘制2D或3D数据 参数 描述 xs, ys X轴,Y轴坐标定点 zs Z值,每一个点的值都是1 zdir 绘制2D集合时使用 ...

  5. Struts2学习笔记整理(三)

    Struts2的输入校验 之前对请求参数的输入校验一般分为两部分:1.客户端校验,也就是我们写js代码去对客户的误操作进行过滤  2.服务端校验, 这是整个应用组织非法数据的最后防线. Struts2 ...

  6. PHP中被忽略的性能优化利器:生成器

    如果是做Python或者其他语言的小伙伴,对于生成器应该不陌生.但很多PHP开发者或许都不知道生成器这个功能,可能是因为生成器是PHP 5.5.0才引入的功能,也可以是生成器作用不是很明显.但是,生成 ...

  7. 三、spring cloud 服务提供与调用

    如何使用eureka服务注册中心,搭建一个简单的服务端注册服务,客户端去调用服务使用. 案例中有三个角色:服务注册中心.服务提供者.服务消费者,eureka单机版启动既可,流程是首先启动注册中心,服务 ...

  8. LBSN中的用户行为模式分析

    LBSN中的用户行为模式分析 zoerywzhou@gmail.com http://www.cnblogs.com/swje/ 作者:Zhouw  2015-12-23   声明: 1)该LBSN的 ...

  9. maven项目启动报:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener 错误解决方法-杜恩德

    如果你是maven项目,tomcat在发布项目的时候没有同时发布maven依赖所添加的jar包, 你需要设置一下eclipse: 项目 -> 属性 -> Deployment Assemb ...

  10. Confluence 持续集成平台部署记录

    1.1 Confluence简介 Confluence是一个专业的企业知识管理与协同软件,也可以用于构建企业wiki.使用简单,但它强大的编辑和站点管理特征能够帮助团队成员之间共享信息.文档协作.集体 ...