-
Notifications
You must be signed in to change notification settings - Fork 439
/
Lowest-Common-Ancestor(Doubling).cpp
101 lines (92 loc) · 1.24 KB
/
Lowest-Common-Ancestor(Doubling).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
#include <cstdio>
using namespace std;
struct vertex
{
int first, depth;
}V[100010];
struct edge
{
int endp, next;
}E[200010];
int ec = 1, ivc;
int fa[100010][25];
void add_edge(int u, int v)
{
E[ec].next = V[u].first;
V[u].first = ec;
E[ec].endp = v;
ec++;
}
void DFS(int u, int prev, int depth)
{
if (fa[u][0] == 0)
{
fa[u][0] = prev;
V[u].depth = depth;
for (int cur = V[u].first; cur != 0; cur = E[cur].next)
{
DFS(E[cur].endp, u, depth + 1);
}
}
}
void init()
{
DFS(1, -1, 1);
for (int i = 1; i <= 20; i++)
{
for (int j = 1; j <= ivc; j++)
{
fa[j][i] = fa[fa[j][i - 1]][i - 1];
}
}
}
int lca(int a, int b)
{
if (V[b].depth > V[a].depth)
{
int temp = a;
a = b;
b = temp;
}
int delta = V[a].depth - V[b].depth, k = 0;
while (delta > 0)
{
if ((delta & 1) == 1)
{
a = fa[a][k];
}
k++;
delta >>= 1;
}
if (a == b)
{
return a;
}
for (int i = 20; i >= 0; i--)
{
if (fa[a][i] != fa[b][i])
{
a = fa[a][i];
b = fa[b][i];
}
}
return fa[a][0];
}
int main()
{
int u, v;
scanf("%d", &ivc);
for (int i = 1; i < ivc; i++)
{
scanf("%d%d", &u, &v);
add_edge(u, v);
add_edge(v, u);
}
init();
while (true)
{
scanf("%d%d", &u, &v);
printf("%d\n", lca(u, v));
}
return 0;
}