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. JVM由浅入深

    运行时数据区域 Java比起C++一个很大的进步就在于Java不用再手动控制指针的delete与free,统一交由JVM管理,但也正因为如此,一旦出现内存溢出异常,不了解JVM,那么排查问题将会变成一 ...

  2. Flask—02-Flask会话控制与模板引擎

    会话控制原理 说明:概念百度说明的很详细,请自行百度 cookie 说明: 由于HTTP协议无状态无连接的特点,导致一个用户在同一网站做连续操作时,需要不断的提供身份信息:为了解决这个问题,我们可以通 ...

  3. JavaScript 表单处理

    表单对象的属性 name action method encoding target elements 表单对象的方法 submit reset 表单元素事件 文本域事件:onFocus(获得焦点) ...

  4. shell脚本中 [-eq] [-ne] [-gt] [-lt] [ge] [le]

    -eq //等于 -ne //不等于 -gt //大于 (greater ) -lt //小于 (less) -ge //大于等于 -le //小于等于 在linux 中 命令执行状态:0 为真,其他 ...

  5. flask笔记(三)Flask 添加登陆验证装饰器报错,及解析

    Flask 添加登陆验证装饰器报错,及解析 写这个之前,是想到一个需求,这个是关于之前写Flask笔记(二)中的一个知识点,路由相关 需求为 : 有一些页面必须是登陆之后才能访问的,比如Shoppin ...

  6. Java分享笔记:自定义枚举类 & 使用enum关键字定义枚举类

    在JDK1.5之前没有enum关键字,如果想使用枚举类,程序员需要根据Java语言的规则自行设计.从JDK1.5开始,Java语言添加了enum关键字,可以通过该关键字方便地定义枚举类.这种枚举类有自 ...

  7. Windows获取物理内存的2种方式 - 随笔记录

    typedef enum _SYSTEM_INFORMATION_CLASS { SystemBasicInformation, SystemProcessorInformation, // obso ...

  8. ABAP术语-Database Rollback

    Database Rollback 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/24/1051238.html Operation tha ...

  9. C#下载局域网共享文件夹中的文件

    在公司的局域网内部,有个主机,共享了几个文件夹给下面的客户机使用. 想要利用这个文件夹上传最新的winform程序版本,每次运行exe的时候检测局域网的软件版本达到更新exe的目的. 这里有个例子,是 ...

  10. Java OOP——第二章 继承

    1. 继承: ●继承是面向对象的三大特征之一,是JAVA实现代码重用的重要手段之一: ●继承是代码重用的一种方式,将子类共有的属性和行为放到父类中: ●JAVA只支持单继承,即每一个类只有一个父类,继 ...