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. 

题目大意:给n个点m条有向边,问有多少个点满足,所有其他点都有一条能到达它的路径。

思路:先用tarjan求强联通分量,缩点,形成树。若有且只有一个点(缩了之后)出度为0,那么所有其他点都能到达它(缩点之后形成的是一个树状结构,出度为0的点为根)。若不止一个点出度为零,说明答案为0(因为这些出度为0的点之间不能互相到达)。

代码(79MS):

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int MAXN = ;
const int MAXE = ; int outdeg[MAXN], pre[MAXN], lowlink[MAXN], sum[MAXN];
int head[MAXN], sccno[MAXN], ecnt, scc_cnt;
int to[MAXE], next[MAXE];
int n, m, dfs_clock;
int stk[MAXN], top; void init() {
memset(sum, , sizeof(sum));
memset(head, , sizeof(head));
memset(outdeg, , sizeof(outdeg));
ecnt = ;
scc_cnt = ;
dfs_clock = ;
} void add_edge(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
} void dfs(int u) {//tarjan
pre[u] = lowlink[u] = ++dfs_clock;
stk[++top] = u;
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(!pre[v]) {
dfs(v);
if(lowlink[u] > lowlink[v]) lowlink[u] = lowlink[v];
} else if(!sccno[v]) {
if(lowlink[u] > pre[v]) lowlink[u] = pre[v];
}
}
if(lowlink[u] == pre[u]) {
++scc_cnt;
while(true) {
int x = stk[top--];
sccno[x] = scc_cnt;
if(x == u) break;
}
}
} int solve() {
for(int i = ; i <= n; ++i)
if(!pre[i]) dfs(i);
for(int u = ; u <= n; ++u) {
++sum[sccno[u]];
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(sccno[u] == sccno[v]) continue;
++outdeg[sccno[u]];
}
}
int ans = ;
for(int i = ; i <= scc_cnt; ++i)
if(outdeg[i] == ) {
if(ans == ) ans = sum[i];
else return ;
}
return ans;
} int main() {
scanf("%d%d", &n, &m);
init();
while(m--) {
int a, b;
scanf("%d%d", &a, &b);
add_edge(a, b);
}
printf("%d\n", solve());
}

POJ 2186 Popular Cows(强联通+缩点)的更多相关文章

  1. POJ 2186 Popular Cows (强联通)

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

  2. POJ 2186 Popular Cows(强联通分量)

    题目链接:http://poj.org/problem?id=2186 题目大意:    每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种 ...

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

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

  4. POJ 2186 Popular Cows(强连通分量缩点)

    题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...

  5. POJ 2186 Popular Cows(Targin缩点)

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

  6. 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)

    poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...

  7. tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows

    缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...

  8. poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】

    题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  9. POJ 2186 Popular cows(Kosaraju+强联通分量模板)

    题目链接:http://poj.org/problem?id=2186 题目大意:给定N头牛和M个有序对(A,B),(A,B)表示A牛认为B牛是红人,该关系具有传递性,如果牛A认为牛B是红人,牛B认为 ...

随机推荐

  1. Vue nodejs商城项目-商品列表页面组件

    data(){        return {            goodsList:[], // 商品列表            priceFilter:[ // 价格区间数组          ...

  2. Java并发编程:线程封闭和ThreadLocal详解

    转载请标明出处: http://blog.csdn.net/forezp/article/details/77620769 本文出自方志朋的博客 什么是线程封闭 当访问共享变量时,往往需要加锁来保证数 ...

  3. selenium之Xpath定位

    1. 绝对定位: driver.find_element_by_xpath("/html/body/div[x]/form/input") x 代表第x个 div标签,注意,索引从 ...

  4. 序列(Sequence)创建、使用、修改和删除

    序列(Sequence)是用来生成连续的整数数据的对象.序列常常用来作为主键中增长列,序列中的可以升序生成,也可以降序生成. 语法结构:创建序列 CREATE SEQUENCE sequence_na ...

  5. 车站分级 (2013noip普及组T4)(树形DP)

    题目描述 一条单向的铁路线上,依次有编号为 1,2,…,n 的 n个火车站.每个火车站都有一个级别,最低为 1 级.现有若干趟车次在这条线路上行驶,每一趟都满足如下要求:如果这趟车次停靠了火车站 x ...

  6. oracle12c管理作业资源的一种方式

    数据库:12.1.0.2,rac,cdb模式 笔者负责移动两个12.1.0.2的cdb集群,一个在aix上,一个在linux上,不幸的是,它们都是混合型,数据有100多T. 由于其它部门交付的时候,已 ...

  7. Git基本使用及工具

    好久没用git管理代码了,最近忙着要实习,一直在看面试题,后天入职了,就提前再复习一下git吧. git比较方便的两个网站,如果你想逼格高就用GitHub(https://github.com/),如 ...

  8. nginx问题之nginx: could not build server_names_hash, you should increase server_names_hash_bucket_size解决方案

    昨天在nginx上部署了一个网站后,发现访问不了,再去访问之前部署的网站,发现都访问不了了,去看下下nginx,发现nginx服务停止了,没有在运行,重启了下服务,发现还是一样,就去看了下nginx的 ...

  9. Phpstudy2018 集成环境配置虚拟域名访问到Index Of 下

    (1)    Phpstudy是一款php集成开发环境 可随意切换Php的版本以及服务器. Phpstudy的网站根目录默认为WWW目录,那么如果我们想通过虚拟域名访问到Index Of目录来便于查看 ...

  10. python集合、函数实例

    集合 1.list ==>允许重复的集合,可修改 2.tuple ==>允许重复的集合,不可修改 3.dict ==> 4.set ==>不允许重复的集合,相当于不可重复的列表 ...