-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCA.cpp
More file actions
39 lines (33 loc) · 734 Bytes
/
LCA.cpp
File metadata and controls
39 lines (33 loc) · 734 Bytes
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
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6, NT = N + 2;
const int LOG = 20; // log n
vector<int> V[NT];
int anc[NT][LOG + 1], pre[NT], post[NT], idx;
void ancestors(int v, int p) {
anc[v][0] = p;
for (int k = 1; k <= LOG; k++) {
anc[v][k] = anc[anc[v][k - 1]][k - 1];
}
idx++;
pre[v] = idx;
for (const int &u : V[v]) {
if (u != v)
ancestors(u, v);
}
post[v] = idx;
}
bool parent(int a, int b) { // czy a jest przodkiem b
return pre[a] <= pre[b] and pre[b] <= post[a];
}
int LCA(int a, int b) {
if (parent(a, b))
return a;
if (parent(b, a))
return b;
for (int k = LOG; k >= 0; k--) {
if (!parent(anc[a][k], b))
a = anc[a][k];
}
return anc[a][0];
}