-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrongconnect.cc
More file actions
47 lines (43 loc) · 866 Bytes
/
strongconnect.cc
File metadata and controls
47 lines (43 loc) · 866 Bytes
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
V(VI) adj;
int i, N, M, cnt;
stack<int> s;
int idx[100001], lowlink[100001];
bool onStack[100001];
void strongconnect(int v) {
idx[v] = i;
lowlink[v] = i;
i++;
s.push(v);
onStack[v] = true;
FOR(it, adj[v]) {
int w = *it;
if (idx[w] < 0) {
strongconnect(w);
}
if (onStack[w]) {
lowlink[v] = min(lowlink[v], lowlink[w]);
}
}
if (lowlink[v] == idx[v]) {
// found a new strongly component
cnt++;
int w;
do {
// w is in this strongly component
w = s.top(); s.pop();
onStack[w] = false;
} while(w != v);
}
}
void tarjan() {
i = 0;
s = stack<int>();
MS0(onStack);
MS1(idx);
cnt = 0;
REP(v, N) {
if (idx[v] < 0) {
strongconnect(v);
}
}
}