#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. <转>如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等

    原文链接:http://www.vaikan.com/use-multiple-cpu-cores-with-your-linux-commands/ 你是否曾经有过要计算一个非常大的数据(几百GB) ...

  2. EF Code First学习笔记

    EF Code First学习笔记 初识Code First EF Code First 学习笔记:约定配置 Entity Framework 复杂类型 Entity Framework 数据生成选项 ...

  3. 在 Asp.NET MVC 中使用 SignalR 实现推送功能

    一,简介Signal 是微软支持的一个运行在 Dot NET 平台上的 html websocket 框架.它出现的主要目的是实现服务器主动推送(Push)消息到客户端页面,这样客户端就不必重新发送请 ...

  4. c/c++ 数字转成字符串, 字符串转成数字

    c/c++ 数字转成字符串, 字符串转成数字 ------转帖 数字转字符串: 用C++的streanstream: #include <sstream> #Include <str ...

  5. 设计模式 工厂-Factory

    在开始笔记之前先推荐一个网站:http://design-patterns.readthedocs.org/zh_CN/latest/index.html 网站对每一个Pattern都有详尽的解说.并 ...

  6. 字符串和数组中split().toString(),join(),splice(),slice(),substr()和substring()

    <!Doctype html> <head> <mate charset="utf-8"> <title>string change ...

  7. Spark on Yarn遇到的几个问题

    1 概述 Spark的on Yarn模式,其资源分配是交给Yarn的ResourceManager来进行管理的,但是目前的Spark版本,Application日志的查看,只能通过Yarn的yarn ...

  8. 第二百零六天 how can I 坚持

    今天爬了趟香山,第三次去了,要征服北京这大大小小的山. 要征服三山五岳,然后...罗娜.哈哈. 爬了趟山好累,人好多. 我的铜钱草. 洗刷睡觉,还是明天给鱼换水吧,好懒.

  9. webconfig文件serviceHostingEnvironment节点出错的解决方法

    在三点五和二版本的配置中可以出现这个节点,但是在4.0是没有的,所以如果框架是4.0的时候要除去这个节点,不然就会报以下错误: Configuration Error Description: An ...

  10. UI进阶 科大讯飞(2) 语音合成(文字转换成语音)

    科大讯飞开放平台.SDK下载.添加静态库.初始化见UI进阶 科大讯飞(1) 语音听写(语音转换成文字) 实现语音合成 功能实现步骤: 导入头文件 创建文字识别对象 指定文字识别后的回调代理对象 开启文 ...