-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLowest Common Ancestor - Sqrt Decomposition.cpp
More file actions
96 lines (74 loc) · 1.49 KB
/
Lowest Common Ancestor - Sqrt Decomposition.cpp
File metadata and controls
96 lines (74 loc) · 1.49 KB
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
#include<bits/stdc++.h>
using namespace std;
const int MAX = 1e5+5;
int block_sz;
int depth[MAX];
int parent[MAX];
int jump_parent[MAX];
vector <int> adj[MAX];
int LCANaive(int u,int v)
{
if(u == v)
return u;
if(depth[u] > depth[v])
swap(u,v);
v = parent[v];
return LCANaive(u,v);
}
int LCA(int u, int v)
{
while(jump_parent[u] != jump_parent[v])
{
if(depth[u] > depth[v])
swap(u,v);
v = jump_parent[v];
}
return LCANaive(u,v);
}
void dfs(int cur, int prev)
{
depth[cur] = depth[prev] + 1;
parent[cur] = prev;
if(depth[cur] % block_sz == 0)
jump_parent[cur] = parent[cur];
else
jump_parent[cur] = jump_parent[prev];
for(int i = 0; i < adj[cur].size(); ++i)
if(adj[cur][i] != prev)
dfs(adj[cur][i], cur);
}
void init(int height)
{
block_sz = sqrt(height);
depth[0] = -1;
dfs(1, 0);
}
int getHeight(int u, int par, int h)
{
int height = h;
for(int v : adj[u])
if(v != par)
height = getHeight(v, u, h+1);
return height;
}
int main()
{
int n, q, u, v;
cin >> n;
for(int i = 1; i < n; ++i)
{
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int height = getHeight(1, 1, 0);
init(height);
cin >> q;
while(q--)
{
cin >> u >> v;
cout << LCA(u, v) << endl;
}
return 0;
}
// NOTE: 1 Based Indexing