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 ...
随机推荐
- web.xml中url-pattern的用法
目录结构: // contents structure [-] url-pattern的三种写法 servlet匹配原则 filter匹配原则 语法错误的后果 参考文章 一.url-pattern的三 ...
- 传统软件和SaaS,差异究竟在哪里
这篇文章从创业起步阶段.产品形态和产品策略.市场竞争格局三个方面比较了中美 SaaS 领域的异同,在文章的最后,作者根据自己在 Box 的工作经历对在国内做 SaaS 的公司提出了四点建议. 我曾有幸 ...
- 如何禁止内部viewPager滑动
众所周知,viewPager是能够滑动的,但有时候我们需要禁止它的滑动(微笑地面对*----*). 情况是这样的: activity中有一个viewPager,viewPager中加入3个Fragme ...
- C#设计模式学习笔记-单例模式
最近在学设计模式,学到创建型模式的时候,碰到单例模式(或叫单件模式),现在整理一下笔记. 在<Design Patterns:Elements of Resuable Object-Orient ...
- 运维之网络安全抓包—— WireShark 和 tcpdump
------------------------------------------------本文章只解释抓包工具的捕获器和过滤器的说明,以及简单使用,应付日常而已----------------- ...
- SPI协议及IO模拟
SPI协议 SPI协议网上资料比较多,但是也比较乱,当初在网上搜集的错误资料导致现在比较混乱. SPI协议资料比较正规的是: 1.SPI的规约协议英文文档,例如<摩托罗拉spi协议规范> ...
- Ubuntu16.04 LTS下apt安装WireShark
Ubuntu16.04 LTS下apt安装WireShark 安装与配置 首先通过apt安装WireShark: $ sudo apt install wireshark 会同时安装许多的依赖包,其中 ...
- Lite Your Android English
https://litesuits.com/ 一些话 简约的背后,往往是复杂 还原面向对象应有的体验,让应对繁多业务所增加的,并未增加. 展开设计理念 Lite每个项目仅几十KB,这相当于你项目中 ...
- [LeetCode] Arranging Coins 排列硬币
You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...
- SQLite3源程序分析之分析器的生成
1.概述 Lemon是一个LALR(1)文法分析器生成工具,与bison和yacc类似,是一个可以独立于SQLite使用的开源的分析器生成工具.而且它使用与yacc(bison)不同的语法规则,可以减 ...