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. ftk学习记(list篇)

    [声明:版权全部,欢迎转载.请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 在開始今天的list主题之前,先看一下icon的执行效果. 今天说的list事实上和这个icon几 ...

  2. Flex/AS3 base64指定字符编码

    public static function base64Encode(str:String, charset:String = "GBK"):String{ if(StringU ...

  3. JAVA入门[7]-Mybatis generator(MBG)自动生成mybatis代码

    一.新建测试项目 新建Maven项目MybatisDemo2,修改pom.xml引入依赖.dependencies在上节基础上新增 <dependency> <groupId> ...

  4. 传统controller与controllerAs

    传统controller与controllerAs(前面为传统,后面为controllerAs,分割线分隔): 路由模块: .state('home.packing', { url: '/packin ...

  5. Swagger文档添加file上传参数写法

    想在swagger ui的yaml文档里面写一个文件上传的接口,找了半天不知道怎么写,终于搜到了,如下: /tools/upload: post: tags: - "tool" s ...

  6. iOS白名单设置

    在做分享.支付的时候需要跳转到对应的app,这里有需要设置的白名单列表<key>LSApplicationQueriesSchemes</key> <array> ...

  7. java表单重复提交常用解决办法

    最近在看些基础的东西,顺便做下笔记.相信大家在平时网页使用中,经常会有按钮重复点击,然后点不动刷新,还有当网络延时比较厉害点了没反应在点击的重复提交.为了避免这种情况,总结了一下4点处理方案 表单重复 ...

  8. Fiori缓存与它的清除

    最近在修改已有的Fiori应用,遇到了缓存上的一点问题,导致对Fiori应用的代码修改不能在前端页面生效.现将自己查到的一篇好资料翻译过来,以供参考.以下为正文. 2017.12.19更新:最近又遇到 ...

  9. Linux权限分析

    我看过网上的一些有关Linux的权限分析,有些说的不够清楚,另外一些说的又太复杂.这里我尽量简单.清楚的把Linux权限问题阐述明白,Linux权限没有那么复杂. Linux权限问题要区分文件权限和目 ...

  10. android 串口开发第二篇:利用jni实现android和串口通信

    一:串口通信简介 由于串口开发涉及到jni,所以开发环境需要支持ndk开发,如果未配置ndk配置的朋友,或者对jni不熟悉的朋友,请查看上一篇文章,android 串口开发第一篇:搭建ndk开发环境以 ...