-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathSCC Tarjan.cpp
70 lines (58 loc) · 1.12 KB
/
SCC Tarjan.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
stack<int> st;
vector<vector<int> > scc;
int low[MAX], disc[MAX], comp[MAX];
int dfs_time;
bool in_stack[MAX];
vi graph[MAX];
int n; // node count indexed from 1
void dfs(int u)
{
low[u] = dfs_time;
disc[u] = dfs_time;
dfs_time++;
in_stack[u] = true;
st.push(u);
int sz = graph[u].size(), v;
for(int i = 0; i < sz; i++)
{
v = graph[u][i];
if(disc[v] == -1)
{
dfs(v);
low[u] = min(low[u], low[v]);
}
else if(in_stack[v] == true)
low[u] = min(low[u], disc[v]);
}
if(low[u] == disc[u])
{
scc.push_back(vector<int>());
while(st.top() != u)
{
scc[scc.size() - 1].push_back(st.top());
in_stack[st.top()] = false;
st.pop();
}
scc[scc.size() - 1].push_back(u);
in_stack[u] = false;
st.pop();
}
}
int tarjan()
{
memset(comp, -1, sizeof(comp));
memset(disc, -1, sizeof(disc));
memset(low, -1, sizeof(low));
memset(in_stack, 0, sizeof(in_stack));
dfs_time = 0;
while(!st.empty())
st.pop();
for(int i = 1; i <= n; i++)
if(disc[i] == -1)
dfs(i);
int sz = scc.size();
for(int i = 0; i < sz; i++)
for(int j = 0; j < (int)scc[i].size(); j++)
comp[scc[i][j]] = i;
return sz;
}