POJ 2186 Popular Cows 强连通分量模板
题意
强连通分量,找独立的块
强连通分量裸题
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream> using namespace std; const int maxn = ;
int n, m;
struct Edge
{
int v, next;
Edge (int v = , int next = ):
v(v), next(next) {}
}e[maxn*];
int head[maxn], label;
int stack[maxn], Scnt;
int belong[maxn], Bcnt;
int dfn[maxn], low[maxn], dfs_clock;
bool instack[maxn];
int siz[maxn], chu[maxn]; void ins(int u, int v)
{
e[++label] = Edge(v, head[u]);
head[u] = label;
} void dfs(int u)
{
dfn[u] = low[u] = ++dfs_clock;
stack[++Scnt] = u;
instack[u] = true;
for (int i = head[u]; i != -; i = e[i].next)
{
int v = e[i].v;
if (!dfn[v])
{
dfs(v);
low[u] = min(low[u], low[v]);
}
else
if (instack[v])
low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u])
{
Bcnt ++;
int v;
do
{
v = stack[Scnt --];
belong[v] = Bcnt;
instack[v] = false;
}while(v != u);
}
} int main()
{
scanf("%d %d", &n, &m);
for (int i = ; i <= n; ++i)
head[i] = -;
label = -;
for (int i = ; i <= m; ++i)
{
int u, v;
scanf("%d %d", &u, &v);
ins(u, v);
}
for (int i = ; i <= n; ++i)
instack[i] = belong[i] = dfn[i] = ;
Scnt = Bcnt = dfs_clock = ;
for (int i = ; i <= n; ++i)
if (!dfn[i])
dfs(i);
for (int i = ; i <= Bcnt; ++i)
siz[i] = chu[i] = ;
for (int i = ; i <= n; ++i)
{
siz[belong[i]] ++;
for (int j = head[i]; j != -; j = e[j].next)
{
int v = e[j].v;
if (belong[v] == belong[i])
continue ;
chu[belong[i]] ++;
}
}
int cnt = , ans;
for (int i = ; i <= Bcnt; ++i)
if (chu[i] == )
cnt ++, ans = siz[i];
if (cnt > )
ans = ;
printf("%d\n", ans);
return ;
}
POJ 2186 Popular Cows 强连通分量模板的更多相关文章
- poj 2186 Popular Cows (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- POJ 2186 Popular Cows --强连通分量
题意:给定一个有向图,问有多少个点由任意顶点出发都能达到. 分析:首先,在一个有向无环图中,能被所有点达到点,出度一定是0. 先求出所有的强连通分支,然后把每个强连通分支收缩成一个点,重新建图,这样, ...
- POJ 2186 Popular Cows(强连通分量缩点)
题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...
- 强连通分量分解 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 【强连通分量Tarjan算法 + 树问题】
题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- 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
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31815 Accepted: 12927 De ...
- POJ 2186 Popular cows(Kosaraju+强联通分量模板)
题目链接:http://poj.org/problem?id=2186 题目大意:给定N头牛和M个有序对(A,B),(A,B)表示A牛认为B牛是红人,该关系具有传递性,如果牛A认为牛B是红人,牛B认为 ...
随机推荐
- ubuntu永久修改主机名
1.查看主机名 在Ubuntu系统中,快速查看主机名有多种方法:其一,打开一个GNOME终端窗口,在命令提示符中可以看到主机名,主机名通常位于“@”符号后:其二,在终端窗口中输入命令:hostname ...
- hdu 2119 Matrix(二分匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2119 Matrix Time Limit: 5000/1000 MS (Java/Others) ...
- MySQL创建相同表和数据命令
创建和表departments结构和数据一样的表departments_t mysql> create table departments_t like departments; Query O ...
- 码源中国.gitignore忽略文件配置
码源中国.gitignore忽略文件配置 ## Ignore Visual Studio temporary files, build results, and ## files generated ...
- C/C++——C语言常用库函数
本文转载自:https://blog.csdn.net/qq_36955347/article/details/71511900 一.数学函数 调用数学函数时,要求在源文件中包下以下命令行: #inc ...
- 百度2017春招<度度熊回家问题>
题目: 一个数轴上共有N个点,第一个点的坐标是度度熊现在位置,第N-1个点是度度熊的家.现在他需要依次的从0号坐标走到N-1号坐标.但是除了0号坐标和N-1号坐标,他可以在其余的N-2个坐标中选出一个 ...
- Oracle数据库(64位) 及 PLSQL(64位)的组合安装【第一篇】
目前PC端普遍使用64位操作系统,64位操作系统在性能上比32位更佳,但是兼容性上则不如32的操作系统,Oracle提供了64和32两种版本,而PLsql工具则只有32位,在这篇文章详细的讲述了如何在 ...
- Python使用opencv
Python配置opencv 原理 Python调用opencv的原理是:opencv编译出共享库文件,python把这个共享库文件作为一个模块加载并使用. 通俗点就是,编译opencv的时候开启py ...
- **CodeIgniter系列 添加filter和helper
filter: 使用CI的hooks来实现filter. 1.在system/application/config/config.php中,把enable_hooks的值改为TRUE $config[ ...
- elk搭建实战
1 安装elasticsearch 1.1安装elasticsearch 相关中文文档:https://es.xiaoleilu.com 下载:从https://www.elastic.co/down ...