-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary_lifting.cpp
117 lines (93 loc) · 1.89 KB
/
binary_lifting.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Vivek Rai
// Blazer_007
#include<bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
typedef long long int ll;
const int hell = 1e9 + 7 ;
const int N = 1000;
const int maxN = 10;
int n;
vector<int> tree[ N ];
vector<int> level( N );
vector<vector<int> > LCA(N, vector<int>(maxN + 1, -1));
void dfs(int src, int par, int lvl)
{
LCA[src][0] = par;
level[src] = lvl;
for(int child : tree[src])
{
if (child != par)
dfs(child, src, lvl + 1);
}
}
void binary_lifting()
{
dfs(1, -1, 0);
for(int j = 1 ; j <= maxN ; j++)
{
for(int i = 1 ; i <= n ; i++)
{
if (LCA[i][j-1] != -1)
{
int par = LCA[i][j-1];
LCA[i][j] = LCA[par][j-1];
}
}
}
}
int getLCA(int a, int b)
{
if (level[b] < level[a])
swap(a,b);
int d = level[b] - level[a];
while(d > 0)
{
int i = log2(d);
b = LCA[b][i];
d -= (1 << i);
}
if (a == b)
return a;
for(int j = maxN ; j >= 0 ; j--)
{
if (LCA[a][j] != -1 and (LCA[a][j] != LCA[b][j]))
{
a = LCA[a][j];
b = LCA[b][j];
}
}
return LCA[a][0];
}
int calcDist(int a, int b)
{
int lca = getLCA(a , b);
return level[a] + level[b] - 2*level[lca];
}
signed main() {
fastio
#ifndef ONLINE_JUDGE
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
cin >> n;
for(int i = 1 ; i < n ; i++)
{
int u,v;
cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
binary_lifting();
int q;
cin >> q;
while(q--)
{
int a, b;
cin >> a >> b;
cout << "LCA of " << a << " and " << b << " : " << getLCA(a,b) << endl;
cout << "Distance between " << a << " and " << b << " : " << calcDist(a,b) << endl;
}
return 0;
}