-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLCA.java
More file actions
57 lines (50 loc) · 1.51 KB
/
LCA.java
File metadata and controls
57 lines (50 loc) · 1.51 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
class LCA {
static final int m = 20; // works for up to 10^6 nodes
static int n;
static ArrayDeque<Integer>[] edges;
static int[] height;
static int[][] jmp;
public static void main(String[] args) {
jmp = new int[m][n];
for(int[] z : jmp) Arrays.fill(z, -1);
height = new int[n];
dfs(0, -1);
for(int j = 1; j < m; ++j) {
for(int i = 0; i < n; ++i) {
int idx = jmp[j - 1][i];
if(idx != -1) jmp[j][i] = jmp[j - 1][idx];
}
}
int a = 0, b = 0, lca = lca(a, b);
int pathLength = height[a] + height[b] - 2*height[lca];
}
static int lca(int a, int b) {
if(height[a] > height[b]) {
int temp = a;
a = b;
b = temp;
}
for(int j = m - 1; j >= 0; --j) {
int newb = jmp[j][b];
if(newb != -1 && height[newb] >= height[a]) b = newb;
}
if(a == b) return a;
for(int j = m - 1; j >= 0; --j) {
int newa = jmp[j][a], newb = jmp[j][b];
if(newa != newb) {
a = newa;
b = newb;
}
}
return jmp[0][a];
}
static void dfs(int idx, int par) {
for(int next : edges[idx]) {
if(next != par) {
height[next] = height[idx] + 1;
jmp[0][next] = idx;
dfs(next, idx);
}
}
}
}