-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11437.cpp
More file actions
95 lines (87 loc) · 1.87 KB
/
Copy path11437.cpp
File metadata and controls
95 lines (87 loc) · 1.87 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
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> tree, ancestor;
vector<int> depth;
void dfs(int now, int parent)
{
ancestor[now].push_back(parent);
depth[now] = depth[parent] + 1;
int cnt = 1;
while (1)
{
int tmp = ancestor[now][cnt - 1];
if (ancestor[tmp].size() < cnt)
break;
ancestor[now].push_back(ancestor[tmp][cnt - 1]);
cnt++;
}
for (int i = 0; i < tree[now].size(); i++)
{
int next = tree[now][i];
if (next != parent)
{
dfs(next, now);
}
}
}
int getLCA(int a, int b)
{
if (depth[a] > depth[b])
{
swap(a, b);
}
int LCA = a;
if (a != b)
{
int maxAncestor = ancestor[b].size() - 1;
while (depth[a] != depth[b])
{
for (int i = maxAncestor; i >= 0; i--)
{
if (depth[a] <= depth[ancestor[b][i]])
{
b = ancestor[b][i];
}
}
}
}
if (a != b)
{
int maxAncestor = ancestor[a].size() - 1;
for (int i = maxAncestor; i >= 0; i--)
{
if (ancestor[a][i] != ancestor[b][i])
{
a = ancestor[a][i];
b = ancestor[b][i];
}
LCA = ancestor[a][i];
}
}
return LCA;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
int N, M, a, b;
cin >> N;
tree.assign(N + 1, vector<int>(0, 0));
ancestor.assign(N + 1, vector<int>(0, 0));
depth.assign(N + 1, 0);
for (int i = 1; i < N; i++)
{
cin >> a >> b;
tree[a].push_back(b);
tree[b].push_back(a);
}
dfs(1, 0);
cin >> M;
while (M--)
{
cin >> a >> b;
cout << getLCA(a, b) << endl;
}
return 0;
}