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模板题的更多相关文章

  1. 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)

    poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...

  2. tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows

    缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...

  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 (强联通)

    id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 655 ...

  5. poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】

    题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  6. POJ 2186:Popular Cows Tarjan模板题

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25945   Accepted: 10612 De ...

  7. poj 2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29908   Accepted: 12131 De ...

  8. [强连通分量] POJ 2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31815   Accepted: 12927 De ...

  9. POJ 2186 Popular Cows(Targin缩点)

    传送门 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31808   Accepted: 1292 ...

随机推荐

  1. ssh整合 小例子

    实现了用户的查插删改操作. 原理:struts负责接收.分发请求.spring采用面向接口编程的思想整合整体框架,负责连接数据库.hibernate负责操作数据库语言. 思路: 1.配置struts的 ...

  2. vue 表单验证省市县三联动

    <el-col :span="24"> <el-form-item label="所在地区" prop="region" ...

  3. 4.oracle正确卸载步骤

    oracle 11g如何完全卸载 方法/步骤1: 停用oracle服务:进入计算机管理,在服务中,找到oracle开头的所有服务,右击选择停止 方法/步骤2: 在开始菜单中,找到oracle-> ...

  4. Introduction to Multi-Threaded, Multi-Core and Parallel Programming concepts

    https://katyscode.wordpress.com/2013/05/17/introduction-to-multi-threaded-multi-core-and-parallel-pr ...

  5. Flutter实战视频-移动电商-24.Provide状态管理基础

    24.Provide状态管理基础 Flutter | 状态管理特别篇 —— Provide:https://juejin.im/post/5c6d4b52f265da2dc675b407?tdsour ...

  6. Eclipse+Maven+TestNg+ReportNg 生成测试报告

    http://blog.csdn.net/a542551042/article/details/46729585

  7. C# 程序软件启动默认管理员权限。

    在vs的Properties目录中找到 app.manifest,将其中level="asInvoker" 改成 level="requireAdministrator& ...

  8. 简易DIV垂直居中阴影层笼罩JS实现

    $(document).ready(init); function init() { var h = $(window).height(); var w = $(window).width(); /* ...

  9. 让webStorm支持自动监听编译scss文件

    前提概要 今日,重装了两波系统,,,之前安装的各种环境都忘光了,重新又踩一次坑的感觉很不舒服,所以记录一下配置自动编译scss一路遇到的坑 一.webstrom run的时候控制台输出的错误中文提示乱 ...

  10. UGUI技术之LayoutGroup布局实现详解

    继续学习unity4.6,unity的LayoutGroup分为三种, Horizontal Layout Group(水平布局)Vertical Layout Group(垂直布局)Grid Lay ...