强连通分量(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 ...
随机推荐
- html --- canvas --- javascript --- 在线画板
canvas功能十分强大,制作一个简易画板易如反掌,主要涉及canvas的画线能力,javascript鼠标点击事件 如有问题请参考:http://www.html5party.com/857.htm ...
- MapReduce 中job.setJarByClass()方法的疑惑
在调试mr实例的时候,遇到如下的情况,如图所示 说明:就是我的mr程序类名称和我设置的setJarByclass()中设置的不一样,但是程序竟然没有报错!!!!当时把我吓尿了 疑惑:如果这样设置的话, ...
- Linux/Unix mac 命令笔记
bg和fg Linux/Unix 区别于微软平台最大的优点就是真正的多用户,多任务.因此在任务管理上也有别具特色的管理思想.我们知道,在 Windows 上面,我们要么让一个程序作为服务在后台一直运行 ...
- bzoj2947: [Poi2000]促销
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 178 Solved: 119[Submit][Status][Discuss] Descriptio ...
- 前N个自然数的随机置换
来自:[数据结构与算法分析——C语言描述]练习2.7 问题描述:假设需要生成前N个自然数的一个随机置换.例如,{4,1,2,5,2}和{3,1,4,2,5}就是合法的置换,但{5,4,1,2,1}却不 ...
- LCA of a Binary Tree
236. Lowest Common Ancestor of a Binary Tree /** * 基础版 * 给定p,q都是在树中 * 有两种情况: * 1. p和q分布在LCA的两侧 * 2. ...
- Web 检测代码 web analysis 开源 open source
1. Grape Web Statistics Grape Web Statistics is a fairly simple piece of analytics software. Grape i ...
- Mellanox OFED2.1-X安装记录
---恢复内容开始--- 1,tcl,tk,gcc-gfortran,libnl-devel依赖包
- Xtrabackup之innobackupex备份恢复详解(转)
add by zhj:对于Xtrabackup2.2来说,已经解决了本文结尾提到的那个bug,当使用--copy-back时,同时加--force-non-empty-directories 即可.这 ...
- POJ 2826 An Easy Problem?!
An Easy Problem?! Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7837 Accepted: 1145 ...