-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarylifting.sublime-snippet
77 lines (71 loc) · 1.86 KB
/
binarylifting.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
<snippet>
<content><![CDATA[
//maxn defines the constraint on number of vertices
//logn is represents ceil( log2(maxn) );
const int MAXN = 10005;
const int logn = 15;
int up[MAXN][logn];
int height[MAXN];
void dfs(vector<vector<int>>& adj, int s, int par = -1) {
for (auto c: adj[s]) {
if (c == par) continue;
height[c] = height[s] + 1;
dfs(adj, c, s);
}
}
int lca(int u, int v) {
if (height[u] < height[v]) swap(u, v);
int diff = height[u] - height[v];
for (int j = logn - 1; ~j; --j) {
if (diff & (1 << j)) {
u = up[u][j];
}
}
if (u == v) return u;
for (int j = logn - 1; ~j; --j) {
if (up[u][j] != up[v][j]) {
u = up[u][j];
v = up[v][j];
}
}
return up[u][0];
}
void binarylifting(){
int n;
cin >> n;
vector<vector<int>> adj(n);
// assumes root as 0, if not change up[root][0] = root and height[root] = 0
for (int i = 0; i < n; ++i) {
int c;
cin >> c;
for (int j = 0; j < c; ++j){
int x;
cin >> x;
//assumes x as child of i, this is crucial because i is parent of x and should be reflected as up[x][0] = i
adj[i].push_back(x);
adj[x].push_back(i);
up[x][0] = i;
}
}
height[0] = 0;
dfs(adj, 0);
up[0][0] = 0;
for (int j = 1; j < logn; ++j) {
for (int i = 0; i < n; ++i) {
up[i][j] = up[up[i][j - 1]][j - 1];
}
}
int q;
cin >> q;
while (q--) {
int u, v;
cin >> u >> v;
cout << lca(u, v) << nline;
}
}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>binarylifting</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>