-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathLCA.cpp
50 lines (39 loc) · 1.07 KB
/
LCA.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
struct LCA
{
#include <vector>
vector<int> L, D;
vector<vector<int> > P;
int N;
LCA(vector<int> & dad, vector<int> & level)
{
N = dad.size();
D = dad, L = level;
int LOG = 1, base = 1;
while(base < N)
LOG++, base <<= 1;
P.resize(N, vector<int>(LOG, -1));
for (int i = 0; i < N; i++)
P[i][0] = D[i];
for (int j = 1; 1 << j < N; j++)
for (int i = 0; i < N; i++)
if (P[i][j - 1] != -1)
P[i][j] = P[P[i][j - 1]][j - 1];
}
int query(int p, int q)
{
int tmp, log, i;
if (L[p] < L[q])
tmp = p, p = q, q = tmp;
for (log = 1; 1 << log <= L[p]; log++);
log--;
for (i = log; i >= 0; i--)
if (L[p] - (1 << i) >= L[q])
p = P[p][i];
if (p == q)
return p;
for (i = log; i >= 0; i--)
if (P[p][i] != -1 && P[p][i] != P[q][i])
p = P[p][i], q = P[q][i];
return D[p];
}
};