[POJ2186]Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 34752   Accepted: 14155

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

 
题目大意:给定一个有向图,求出有多少点满足所有点可以间接或直接地到它。
试题分析:Tarjan缩点,然后求出哪个点出度为0就好了,输出其大小。(因为缩点后是DAG)
     如果有>1个出度为0那么就肯定没有了。
 
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=500001;
const int INF=999999;
int N,M;
int dfn[MAXN],low[MAXN];
int que[MAXN]; bool vis[MAXN];
vector<int> vec[MAXN];
int outdu[MAXN];
int tar[MAXN];
int tot,tmp,Col;
int size[MAXN]; void Tarjan(int x){
++tot; dfn[x]=low[x]=tot;
vis[x]=true; que[++tmp]=x;
for(int i=0;i<vec[x].size();i++){
int to=vec[x][i];
if(!dfn[to]){
Tarjan(to);
low[x]=min(low[x],low[to]);
}
else if(vis[to]) low[x]=min(dfn[to],low[x]);
}
if(dfn[x]==low[x]){
++Col; tar[x]=Col;
vis[x]=false;
while(que[tmp]!=x){
int k=que[tmp];
tar[k]=Col; vis[k]=false;
tmp--;
}
tmp--;
}
}
int ans,anst;
int main(){
N=read(),M=read();
for(int i=1;i<=M;i++){
int u=read(),v=read();
vec[u].push_back(v);
}
for(int i=1;i<=N;i++) if(!dfn[i]) Tarjan(i);
for(int i=1;i<=N;i++){
int col=tar[i];size[col]++;
for(int j=0;j<vec[i].size();j++){
if(tar[vec[i][j]]!=col)
outdu[col]++;
}
}
for(int i=1;i<=Col;i++){
if(!outdu[i])
ans+=size[i],anst++;
}
if(anst>1) printf("0\n");
else printf("%d\n",ans);
}

【图论】Popular Cows的更多相关文章

  1. POJ 2186 Popular Cows(Targin缩点)

    传送门 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31808   Accepted: 1292 ...

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

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

  3. poj 2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29908   Accepted: 12131 De ...

  4. [强连通分量] POJ 2186 Popular Cows

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

  5. POJ 2186 Popular Cows(强连通)

                                                                  Popular Cows Time Limit: 2000MS   Memo ...

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

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

  7. poj 2186 Popular Cows【tarjan求scc个数&&缩点】【求一个图中可以到达其余所有任意点的点的个数】

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27698   Accepted: 11148 De ...

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

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

  9. POJ 2186 Popular Cows (强联通)

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

随机推荐

  1. 基于 Docker 的 Zabbix 微服务系统

    zabbix 官网提供一个镜像 [ zabbix-appliance ], 可以直接拉起一个 zabbix-server. 但是数据库无法分离出来. 本实践使用 zabbix 官方提供的 Docker ...

  2. Angular2.0 基础:双向数据绑定 [(ngModel)]

    在属性绑定中,值从模型到屏幕上的目标属性 (property). 通过把属性名括在方括号中来标记出目标属性,[]. 这是从模型到视图的单向数据绑定. 而在事件绑定中,值是从屏幕上的目标属性 到 mod ...

  3. SQL Workbench/J

    最近测试segment, 使用了一个新的DB--SQL Workbench/J, 参考文档:http://docs.aws.amazon.com/redshift/latest/mgmt/connec ...

  4. linux下进行base64编码解码

    1.编码 2.解码

  5. Python3 嵌套函数

    嵌套函数: 函数体内用def定义函数 注意:函数体中调用其他函数不算嵌套函数,只能是函数的调用 简单的嵌套函数: 输出结果:

  6. libSVM笔记之(一)在matlab环境下安装配置libSVM

    本文为原创作品,转载请注明出处 欢迎关注我的博客:http://blog.csdn.net/hit2015spring和http://www.cnblogs.com/xujianqing 台湾林智仁教 ...

  7. python基础===* 解包,格式化输出和print的一点知识

    python3中的特性: >>> name = "botoo" >>> print(f"my name is {name}" ...

  8. python实战===图片转换为字符的源码(转)

    #cmd执行命令为>>> python xx.py pic.png#-*- coding:utf-8 -*- from PIL import Image import argpars ...

  9. nodejs 使用redis 管理session

    一.在开发机安装redis并远程连接 因本人的远程开发机配置原因,使用jumbo安装redis 首先登录开发机,并使用jumbo 安装redis:jumbo install redis 查看redis ...

  10. Python爬虫音频数据

    一:前言 本次爬取的是喜马拉雅的热门栏目下全部电台的每个频道的信息和频道中的每个音频数据的各种信息,然后把爬取的数据保存到mongodb以备后续使用.这次数据量在70万左右.音频数据包括音频下载地址, ...