-
Notifications
You must be signed in to change notification settings - Fork 3
/
Tarjan's strongly connected components.cpp
110 lines (92 loc) · 2.1 KB
/
Tarjan's strongly connected components.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <cstdio>
#include <cmath>
#include <iostream>
#include <string.h> // For memset function
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <string>
#include <algorithm>
#include <bitset>
#include <sstream>
#include <map>
using namespace std;
#define FOR( i, L, U ) for(int i=(int)L ; i<=(int)U ; i++ )
#define FORD( i, U, L ) for(int i=(int)U ; i>=(int)L ; i-- )
#define SQR(x) ((x)*(x))
#define INF 99999999
#define SZ size()
#define PB push_back
#define PF push_front
#define READ(filename) freopen(filename, "r", stdin);
#define WRITE(filename) freopen(filename, "w", stdout);
typedef long long LL;
typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<double> VD;
typedef vector<string> VS;
typedef map<int, int> MII;
typedef map<string, int> MSI;
typedef map<string, char> MSC;
#define WHITE 0
#define GRAY 1
#define BLACK 2
int nodes, edges;
VVI g;
VI color, dfsNum, dfsLow;
int nodeNum;
stack<int> s;
VI inStack;
void strongConnect(int u);
void Tarjan()
{
color = VI(nodes+1, WHITE);
dfsNum = VI(nodes+1);
dfsLow = VI(nodes+1);
inStack = VI(nodes+1, false);
nodeNum = 0;
FOR(u, 1, nodes) {
if(color[u] == WHITE)
strongConnect(u);
}
}
void strongConnect(int u)
{
dfsNum[u] = dfsLow[u] = nodeNum++;
color[u] = GRAY;
s.push(u); inStack[u] = true;
FOR(i, 0, g[u].SZ-1) {
int v = g[u][i];
if(color[v] == WHITE) {
strongConnect(v);
dfsLow[u] = min(dfsLow[u], dfsLow[v]);
}
else if(inStack[v] == true)
dfsLow[u] = min(dfsLow[u], dfsNum[v]);
}
if(dfsNum[u] == dfsLow[u]) {
int w = -1;
while(w != u) {
w = s.top(); s.pop(); inStack[w] = false;
cout << w << " ";
}
cout << "\n";
}
}
int main()
{
READ("input.txt");
// WRITE("output.txt");
int u, v;
while(cin >> nodes >> edges) {
g = VVI(nodes+1);
FOR(i, 1, edges) {
cin >> u >> v;
g[u].PB(v);
}
Tarjan();
cout << "\n";
}
return 0;
}