forked from DanielW48/HackPack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeastCommonAncestor.java
More file actions
45 lines (39 loc) · 890 Bytes
/
LeastCommonAncestor.java
File metadata and controls
45 lines (39 loc) · 890 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
40
41
42
43
44
45
class LeastCommonAncestor {
static int n;
static int[] par, height;
static int[][] jmp;
public static void main(String[] args) {
par = new int[n];
height = new int[n];
jmp = new int[31][n];
for(int i = 0; i < n; ++i) jmp[0][i] = par[i];
for(int j = 1; j < 31; ++j){
for(int i = 0; i < n; ++i){
if(jmp[j - 1][i] < 0) jmp[j][i] = -1;
else jmp[j][i] = jmp[j - 1][jmp[j - 1][i]];
}
}
}
static int lca(int a, int b) {
if(height[a] > height[b]){
int temp = a;
a = b;
b = temp;
}
for(int j = 30; j >= 0; --j) {
int to = jmp[j][b];
if(to >= 0 && height[to] >= height[a]) b = to;
}
if(a == b) return a;
for(int j = 30; j >= 0; --j) {
int toA = jmp[j][a];
int toB = jmp[j][b];
if(toA < 0 || toB < 0) continue;
if(toA != toB) {
a = toA;
b = toB;
}
}
return par[a];
}
}