POJ 2186 Popular Cows(Targin缩点)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 31808 | Accepted: 12921 |
Description
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
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
Output
Sample Input
3 3 1 2 2 1 2 3
Sample Output
1
Hint
思路
题意:给出一对牛之间的羡慕关系,并且当A羡慕B,B羡慕C时,可以认为C也被A羡慕。问N头牛中,有几头牛被其他所有牛羡慕。
题解:根据样例可以看出,这个图不是DAG图,但是我们可以通过targin缩点,使之成为DAG图,对于DAG图,我们知道,如果一头牛有出度,那么它就不是被其他所有牛仰慕的牛,如果其出度为0那么其有可能成为被其他所有牛仰慕的牛,但是当出度为0的牛超过1时,便不存在被除自身外其他牛仰慕的牛,因为肯定有另外一头出度为0的牛不仰慕它。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 10005; int tot, top, scc_cnt, index; int head[maxn], dfn[maxn], low[maxn], outde[maxn], belong[maxn], st[maxn], inst[maxn], cnt[maxn]; struct Edge { int v, next; } edge[maxn*maxn]; void init() { tot = top = index = scc_cnt = 0; memset(head, -1, sizeof(head));memset(belong, 0, sizeof(belong)); memset(dfn, 0, sizeof(dfn));memset(low, 0, sizeof(low)); memset(st, 0, sizeof(st));memset(inst, 0, sizeof(inst)); memset(outde, 0, sizeof(outde)); memset(cnt, 0, sizeof(cnt)); } void addedge(int u, int v) { edge[tot] = (Edge) { v, head[u] }; head[u] = tot++; } void targin(int u) { int v; dfn[u] = low[u] = ++index; st[++top] = u; inst[u] = 1; for (int i = head[u];i != -1;i = edge[i].next) { v = edge[i].v; if (!dfn[v]) { targin(v); low[u] = min(low[u],low[v]); } else if (inst[v]) low[u] = min(low[u],dfn[v]); } if (dfn[u] == low[u]) { scc_cnt++; do { v = st[top--]; inst[v] = 0; belong[v] = scc_cnt; cnt[scc_cnt]++; } while (u != v); } } int main() { int N, M, u, v, res, sum = 0; init(); scanf("%d%d", &N, &M); for (int i = 0; i < M; i++) { scanf("%d%d", &u, &v); addedge(u, v); } for (int i = 1; i <= N; i++) if (!dfn[i]) targin(i); for (int i = 1; i <= N; i++) { for (int j = head[i]; ~j; j = edge[j].next) { int v = edge[j].v; if (belong[i] != belong[v]) { outde[belong[i]]++; } } } for (int i = 1; i <= scc_cnt; i++) { if (!outde[i]) { res = i; sum++; } } if (sum > 1) printf("0\n"); else printf("%d\n", cnt[res]); }
POJ 2186 Popular Cows(Targin缩点)的更多相关文章
- POJ 2186 Popular cows(SCC 缩点)
Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10, ...
- POJ 2186 Popular Cows tarjan缩点算法
题意:给出一个有向图代表牛和牛喜欢的关系,且喜欢关系具有传递性,求出能被所有牛喜欢的牛的总数(除了它自己以外的牛,或者它很自恋). 思路:这个的难处在于这是一个有环的图,对此我们可以使用tarjan算 ...
- 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)
poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...
- poj 2186 Popular Cows (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows
缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...
- POJ 2186 Popular Cows (强联通)
id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 655 ...
- poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】
题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- poj 2186 Popular Cows【tarjan求scc个数&&缩点】【求一个图中可以到达其余所有任意点的点的个数】
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27698 Accepted: 11148 De ...
- poj 2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29908 Accepted: 12131 De ...
随机推荐
- 3.1 js基本概念
js中的语法大量借鉴于C以及其他类C语言(Java,Perl). js中一切(变量.函数名.操作符等等)都区分大小写.如"var a;"中的变量a跟"var A;&quo ...
- 在Android中,使用Kotlin的 API请求简易方法
原文标题:API request in Android the easy way using Kotlin 原文链接:http://antonioleiva.com/api-request-kotli ...
- oncopy="document.selection.empty()"跟oncopy="return false"什么区别?
实现效果一样,禁止复制. 区别: oncopy="document.selection.empty()" 没禁止,只是把它复制的内容,变成空了: oncopy="ret ...
- CentOS如何查看硬盘品牌型号等具体信息
首先使用smartctl --all /dev/sda 指令来检查硬盘信息,该指令CentOS自带,得到的结果可能如下: smartctl 5.43 2012-06-30 r3573 [x86_64- ...
- 梳理delegate相关概念
一.前言 可能项目规模较小,项目中除了增删改查就只剩下业务流程,以前都没怎么弄明白的东西时间长了就越发的模糊了... 二.使用场景 MSDN:delegate 是一种可用于封装命名或匿名方法的引用类型 ...
- 在output 子句和 scope_identity() 混合使用的时候的注意事项
无意睹到一篇旧文档 SR0008:考虑使用 SCOPE_IDENTITY 代替 @@IDENTITY :https://msdn.microsoft.com/zh-cn/library/dd17212 ...
- mongodb简介(转)
1.简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数 ...
- swfupload 相关配置
部署在IIS上出现404: 修改 C:\Windows\System32\inetsrv\config\applicationHost.config 文件 连续查找requestFiltering,往 ...
- sublime text 3 快捷键大全以及配置编译环境
Sublime text 3是码农最喜欢的代码编辑器,每天和代码打交道,必先利其器,掌握基本的代码编辑器的快捷键,能让你打码更有效率.刚开始可能有些生疏,只要花一两个星期坚持使用并熟悉这些常用的快捷键 ...
- java中可定制的序列化过程 writeObject与readObject
来源于:[http://bluepopopo.iteye.com/blog/486548] 什么是writeObject 和readObject?可定制的序列化过程 这篇文章很直接,简单易懂.尝试着翻 ...