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. block与inline,inline和inline-block,块级和行内元素,行内替换和行内非替换元素

    block:块级元素默认display属性为block:无论块内内容有多少,总是占满一行: inline:行内元素默认display属性为inline:只占据块内的内容的大小,不会占满一整行: inl ...

  2. Struts2 第五讲 -- Struts2与Servlet的API解耦

    为了避免与 Servlet API 耦合在一起, 方便 Action 做单元测试, Struts2 对 HttpServletRequest, HttpSession 和 ServletContext ...

  3. 为什么有IP还需要硬件地址,或者说为什么有硬件地址还需要IP

    只用MAC 虽然每个设备都有唯一的硬件地址,但不都是MAC格式. 只用MAC的话理论上是可行的,但是其中 兼容不同的硬件地址,处理起来是非常困难的.而且数据链路层也没有必要处理网络层的逻辑. 只用IP ...

  4. 嵌入式:UCOSIII的使用(17.01.24补充)

    0.一些移植.系统相关 OS_CFG_APP.H /* --------------------- MISCELLANEOUS ------------------ */ #define OS_CFG ...

  5. js的事件流你真的弄明白了吗?

    当浏览器发展到第四代时候,浏览器开发团队遇到了一个有意思的问题:页面的哪一部分会拥有某个特地的事件?要明白这个问题问的是什么,可以想象画在纸上的一组同心圆,如果你把手指放在圆心上,那么你的手指指向的不 ...

  6. Zookeeper 面试总结

    1:Zookeeper是什么? 答:ZooKeeper是一个开源的分布式协调服务,是集群的管理者,监视集群节点反馈信息进行下一步合理操作. Zookeeper提供的服务:管理用户程序提交的数据:为用户 ...

  7. 关于J2EE里面getContextPath()和getRealPath()的区别

    一直老搞不清楚这两个方法的区别,只知道他们都是拿来获取地址的.今天特意写了个小demo试了一下,代码如下: @Override protected void service(HttpServletRe ...

  8. django+xadmin在线教育平台(十七)

    8-1 课程列表 拷贝课程列表页到template目录 创建课程相关的urls.py Mxonline2/urls.py中声明包含到course的url中: # 课程app的url配置 url(r&q ...

  9. PHP无法用下标访问

    php数组分为普通数组和关联数组,普通数组可以用下标访问,而关联数组不可以.

  10. Linux 用户 和 组 快速了解

    1用户 (Linux中“只有超级管理员”才有权限操作 用户 和组) 1.1添加用户 useradd 命令 例如 :useradd hly //添加了一个新账户 hly 用户添加后 会存放在一个文件中, ...