强连通分量(LRJ训练指南)
#include <iostream>
#include <queue>
#include <string>
#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
const int maxn = 10000;
int gn, gm; vector<int> gg[maxn];
int pre[maxn], low[maxn], sccno[maxn], dfs_clock, scc_cnt; stack<int> gs; void dfs(int u) {
int i;
pre[u] = low[u] = ++dfs_clock;
gs.push(u);
for(i = 0; i < (int)gg[u].size(); i++) {
int v = gg[u][i];
if(!pre[v]) {
dfs(v);
low[u] = min(low[u], low[v]);
}
else if(!sccno[v]) {
low[u] = min(low[u], low[v]);//min(low[u], low[v]);
}
}
if(low[u] == pre[u]) {
scc_cnt++;
for(;;) {
int x = gs.top();
gs.pop();
sccno[x] = scc_cnt;
if(x == u) break;
}
}
} void find_scc(int n) {
dfs_clock = scc_cnt = 0;
memset(sccno, 0, sizeof(sccno));
memset(pre, 0, sizeof(pre));
for(int i = 1; i <= n; i++) {
if(!pre[i]) dfs(i);
}
} void print() {
for(int i = 1; i <= gn; i++) {
printf("i = %d -> %d\n", i, sccno[i]);
}
} int main()
{
int from, to;
while(scanf("%d%d", &gn, &gm) ==2) {
for(int i = 0; i < gm; i++) {
scanf("%d%d", &from, &to);
gg[from].push_back(to);
}
find_scc(gn);
print();
}
return 0;
} /**
11 16
1 2
2 3
2 5
3 4
4 3
4 7
5 2
5 4
5 6
6 7
6 10
7 8
8 9
9 10
10 11
11 6
***/
强连通分量(LRJ训练指南)的更多相关文章
- 训练指南 UVALive - 4287 (强连通分量+缩点)
layout: post title: 训练指南 UVALive - 4287 (强连通分量+缩点) author: "luowentaoaa" catalog: true mat ...
- 训练指南 UVALive - 3523 (双联通分量 + 二分图染色)
layout: post title: 训练指南 UVALive - 3523 (双联通分量 + 二分图染色) author: "luowentaoaa" catalog: tru ...
- 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)
layout: post title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP) author: "luowentaoaa" catalog: true ...
- HDU1269 迷宫城堡(裸强连通分量)
Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称某通道连通了A ...
- hdu1269强连通分量入门题
https://vjudge.net/contest/156688#problem 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<= ...
- HDU 1269 迷宫城堡(判断有向图强连通分量的个数,tarjan算法)
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 训练指南 UVALive - 5135 (双连通分量)
layout: post title: 训练指南 UVALive - 5135 (双连通分量) author: "luowentaoaa" catalog: true mathja ...
- HDU1269 迷宫城堡 —— 强连通分量
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1269 迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) M ...
- HDU_1269_tarjan求强连通分量
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
随机推荐
- AI钻石天鹅风格
第1步:描绘轮廓 你需要对你的设计有个总体的概念.利用照片和钢笔工具(P)描出轮廓.把填充颜色设为无,描边颜色设为黑色,1pt 粗细.这将作为你完成剩下设计的指导.编组 (Ctrl+G)你的线条并在图 ...
- CreateProcess error=206, The filename or extension is too long"的一个解决方案
在实际项目中我使用antrun 和 closure-compiler压缩JS项目.然后我就使用如下代码: 首先加入依赖. <dependency> <groupId>com.g ...
- [转] 苹果所有常用证书,appID,Provisioning Profiles配置说明及制作图文
转自holydancer的CSDN专栏,原文地址:http://blog.csdn.net/holydancer/article/details/9219333 首先得描述一下各个证书的定位,作用,这 ...
- LINQ标准查询操作符(一)——select、SelectMany、Where、OrderBy、OrderByDescending、ThenBy、ThenByDescending和Reverse
一.投影操作符 1. Select Select操作符对单个序列或集合中的值进行投影.下面的示例中使用select从序列中返回Employee表的所有列: //查询语法 var query = fro ...
- Apache Spark是什么?
简单地说, Spark是发源于美国加州大学伯克利分校AMPLab的大数据分析平台,它立足于内存计算,从多迭代批量处理出发,兼顾数据仓库. 流处理和图计算等多种计算范式,是大数据系统领域的全栈计算平台. ...
- 未找到与约束contractname Microsoft.VisualStudio.Utilities.IContentTypeRegistryService
在項目中遇到的問題,網上找到的答案,做個記錄, 项目能打开,但是当要在项目中查看文件时弹出 未找到与约束 Microsoft.VisualStudio.Utilities.IContentTypeRe ...
- C++11for循环
[C++11for循环] for 述句将允许简单的范围迭代,引用或非引用形式均可: 参考:http://zh.wikipedia.org/wiki/C++0x
- AJAX制作JSON格式的实时更新数据的方法
之前有写过这样的文章,但是出现了几个问题,第一,如果每秒都像数据库发送请求势必会造成服务器的压力过大,第二,如果使用JS的话,是不可以取得系统时间的,因为JS运行在客户端,所以只能取得客户端时间, 如 ...
- C# Devexpress学习--LabelControl
A LabelControl can display an image (regular or animated GIF file). Different images can be provided ...
- HDU 1392 Surround the Trees 构造凸包
又是一道模板题 #include <iostream> #include <cstring> #include <cstdlib> #include <cst ...