POJ - 2186 Popular Cows tarjain模板题
http://poj.org/problem?id=2186
首先求出所有的强连通分量,分好块。然后对于每一个强连通分量,都标记下他们的出度。那么只有出度是0 的块才有可能是答案,为什么呢?因为既然你有了出度,那么就是指向了另外一个块,那么你就不能指望那个块也指向你了,因为这样会形成环,所以肯定有一个块的cow不支持你,所以你这个块就不会是popular cow
那么就有两种情况,
①、出度是0的块的个数是1个,那么就是唯一的答案。
②、出度是0的快的个数有多个,那么答案是0, 因为至少也有一个块不支持你。
不会存在出度为0的个数是0这样的,起码都会有一个块出度是0.
记得清空各种数组。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e5 + ;
struct Edge {
int u, v, w, tonext;
}e[maxn * ];
int first[maxn], num;
void addEdge(int u, int v) {
++num;
e[num].u = u, e[num].v = v;
e[num].tonext = first[u];
first[u] = num;
}
int DFN[maxn], low[maxn], st[maxn], top, when, vis[maxn];
int id[maxn], toSelId, out[maxn], sum[maxn];
void tarjan(int cur, int fa) {
DFN[cur] = low[cur] = ++when; // 时间戳
st[++top] = cur; //进栈
vis[cur] = true;
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
if (!DFN[v]) { //没访问过
tarjan(v, cur);
low[cur] = min(low[cur], low[v]);
} else if (vis[v]) { // 访问过,而且还在栈里
low[cur] = min(low[cur], DFN[v]);
}
}
if (low[cur] == DFN[cur]) { //这个是强连通分量的根节点。
++toSelId;
do {
id[st[top]] = toSelId;
sum[toSelId]++;
// printf("%d ", st[top]);
vis[st[top]] = false;
top--;
} while (cur != st[top + ]);
// printf("\n");
}
}
void sloveTarjan(int n) { //防止开始枚举的节点没有出边,暴力枚举每一个节点
memset(low, , sizeof low);
memset(DFN, , sizeof DFN);
memset(vis, , sizeof vis);
memset(id, , sizeof id);
memset(out, , sizeof out);
memset(sum, , sizeof sum);
top = when = toSelId = ;
for (int i = ; i <= n; ++i) {
if (!DFN[i]) {
tarjan(i, i);
}
}
}
int n, m;
void work() {
// int n, m;
// scanf("%d%d", &n, &m);
num = ;
memset(first, , sizeof first);
for (int i = ; i <= m; ++i) {
int u, v;
scanf("%d%d", &u, &v);
addEdge(u, v);
}
sloveTarjan(n);
for (int i = ; i <= n; ++i) {
for (int j = first[i]; j; j = e[j].tonext) {
int v = e[j].v;
if (id[i] == id[v]) continue;
out[id[i]]++;
}
}
int has = ;
int ans;
for (int i = ; i <= toSelId; ++i) {
if (out[i] == ) {
has++;
ans = i;
}
}
if (has >= ) {
printf("0\n");
// cout << 0 << endl;
return;
}
printf("%d\n", sum[ans]);
// cout << sum[ans] << endl;
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
while (scanf("%d%d", &n, &m) != EOF) work();
return ;
}
POJ - 2186 Popular Cows tarjain模板题的更多相关文章
- 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)
poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...
- tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows
缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...
- poj 2186 Popular Cows (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- 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模板题
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25945 Accepted: 10612 De ...
- poj 2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29908 Accepted: 12131 De ...
- [强连通分量] POJ 2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31815 Accepted: 12927 De ...
- POJ 2186 Popular Cows(Targin缩点)
传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31808 Accepted: 1292 ...
随机推荐
- PICT实现组合测试用例
成功安装后,在命令行中输入命令pict: 可以看到pict命令的一些选项: /o:N 组合数,默认值为2,即pict生成的测试用例集中每条测试数据会有两个值与其他测试集是不同的: /d:C 值 ...
- Yii的缓存机制之动态缓存
当整个页面被缓存,但只有小部分区域需要根据不同的条件设置不同的信息.(例如商品的详细页面的缓存中用户名是动态的)这里就需要设置动态缓存. 首先在被缓存的模板中使用renderDynamic进行动态渲染 ...
- Notepad++安装xml插件
环境: win7 64位 Notepad++7.3.3 原生的Notepad++不自带xml文件的插件,所以在显示xml文件时并不分行(如下图所示),对于用户编辑,查看的操作而言,并不友好,所以需要安 ...
- python库学习笔记——分组计算利器:pandas中的groupby技术
最近处理数据需要分组计算,又用到了groupby函数,温故而知新. 分组运算的第一阶段,pandas 对象(无论是 Series.DataFrame 还是其他的)中的数据会根据你所提供的一个或多个键被 ...
- 装饰器 decorator
装饰器 def document_it(func): def new_function(*args, **kwargs): print('Running function:', func.__name ...
- C++之全局函数和成员函数互相转换
解析:成员函数会用this指针自动隐藏第一个操作数(左操作数) 1.把全局函数转化成成员函数,通过this指针隐藏左操作数. Test add(Test &t1,Test &t2) ...
- HDU3065(AC自动机入门题)
病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- 使用基于Caffe的MobileNet分类踩坑备忘录
首先要帮Caffe甩个锅:Caffe对图像处理进行了很高明的封装,以protobuffer形式组织的搭积木式的网络构建也很灵活方便,这里的坑都是自己腿不好,走路不稳崴进去的. 1. Caffe的一个i ...
- HDU - 1016 Prime Ring Problem 经典素数环
Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...
- HTML学习笔记(六)TCP/IP
TCP/IP 是供已连接因特网的计算机进行通信的通信协议. 在 TCP/IP 中包含一系列用于处理数据通信的协议: TCP (传输控制协议) - 应用程序之间通信 UDP (用户数据包协议) - 应用 ...