#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训练指南)的更多相关文章

  1. 训练指南 UVALive - 4287 (强连通分量+缩点)

    layout: post title: 训练指南 UVALive - 4287 (强连通分量+缩点) author: "luowentaoaa" catalog: true mat ...

  2. 训练指南 UVALive - 3523 (双联通分量 + 二分图染色)

    layout: post title: 训练指南 UVALive - 3523 (双联通分量 + 二分图染色) author: "luowentaoaa" catalog: tru ...

  3. 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)

    layout: post title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP) author: "luowentaoaa" catalog: true ...

  4. HDU1269 迷宫城堡(裸强连通分量)

    Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称某通道连通了A ...

  5. hdu1269强连通分量入门题

    https://vjudge.net/contest/156688#problem 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<= ...

  6. HDU 1269 迷宫城堡(判断有向图强连通分量的个数,tarjan算法)

    迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. 训练指南 UVALive - 5135 (双连通分量)

    layout: post title: 训练指南 UVALive - 5135 (双连通分量) author: "luowentaoaa" catalog: true mathja ...

  8. HDU1269 迷宫城堡 —— 强连通分量

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1269 迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    M ...

  9. HDU_1269_tarjan求强连通分量

    迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. eclipse 恢复被删除的文件

    即使你在项目中删除了某一文件,该文件的相关信息仍会保存在本地历史记录中.这就使得你可以恢复那些在项目或文件夹中已被删除的文件.如果恢复某一被删除的文件,则首先在Navigator视图中选择该文件先前所 ...

  2. MySQL_PHP学习笔记_2015_0923_MySQL如何开启事件

    1. 查看事件状态>>>>>>>>>>>>>>>>>>>>>>> ...

  3. C# 实现无焦点窗体(转载)

    #region 无焦点窗体 [System.Runtime.InteropServices.DllImport("user32.dll")] private extern stat ...

  4. JavaScript基础知识整理(1)数组

    第一:创建. 1,var arr= new Array(); //数组为空.长度为0. arr[0]="apple"; arr[1]="orange"; arr ...

  5. Netmask v. Address Prefix Length

    Netmask Address Prefix Length Hosts / Class C's / Class B's / Class A's (Class C) / / , / , / , / , ...

  6. WS之cxf简单实现

    1.服务端实现: 1.1 定义接口,用@WebService修饰: /** @WebService 所修饰的接口,那么接口里面的方法全部都属于web的服务  */ @WebService public ...

  7. Java基础 —— JavaScript

    Javascript:基于对象与事件驱动的脚本语言,主要用于客户端 特点: 交互性:信息动态交互. 安全性:不能访问本地硬盘. 跨平台性:只要有浏览器就支持Javascript,与平台无关. Java ...

  8. 20160512关于mac安装caffe的记录

    记得2015年在mac系统上安装过一次caffe,非常顺利,但是最近群里许多同学反映mac安装caffe出现了各种问题,同时我也在帮助别人安装caffe的时候也遇到了一些坑,不再像以前这么顺利了.估计 ...

  9. 适应所有浏览器的cookie

    //设置cookie的方法 weiyingfunction SetCookie(a, b) {        var d = new Date();    var v = arguments;    ...

  10. spring 解析配置文件问题

    问题描述 2014-02-25 16:39:36.068 [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2] WARN ...