-
Notifications
You must be signed in to change notification settings - Fork 0
/
kosaraju.sublime-snippet
78 lines (75 loc) · 1.25 KB
/
kosaraju.sublime-snippet
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
<snippet>
<content><![CDATA[
class kosaraju
{
public:
vector < vector<int>>adj;
vector<vector<int>>Adj;
stack<int>stk;
vector<bool>visited;
vector<vector<int>>clusters;
void dfs(int curr)
{
if (visited[curr])
return;
visited[curr] = true;
for (auto it : adj[curr])
{
dfs(it);
}
stk.push(curr);
};
void rec(int curr, vector<int>&v)
{
if (visited[curr])
return;
visited[curr] = true;
v.push_back(curr);
for (auto it : Adj[curr])
{
rec(it, v);
}
}
kosaraju(vector<vector<int>>&adj)
{
this->adj = adj;
int N = adj.size();
visited.resize(N, false);
for (int i = 0; i < N; i++)
{
if (!visited[i])
{
dfs(i);
}
}
Adj.resize(N);
for (int i = 0; i < N; i++)
{
for (auto it : adj[i])
{
Adj[it].push_back(i);
}
}
for (int i = 0; i < N; i++)
{
visited[i] = false;
}
while(!stk.empty())
{
int i=stk.top();
stk.pop();
if (!visited[i])
{
vector<int>cluster;
rec(i, cluster);
clusters.push_back(cluster);
}
}
}
};
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>kosaraju</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>