-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlca.java
More file actions
217 lines (177 loc) · 5.61 KB
/
lca.java
File metadata and controls
217 lines (177 loc) · 5.61 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
Lowest Common Ancestor (LCA) using Binary Lifting.
Preprocesses a tree to answer LCA queries efficiently.
Key operations:
- addEdge(u, v): Add undirected edge to tree
- build(root): Preprocess tree with given root - O(n log n)
- query(u, v): Find LCA of nodes u and v - O(log n)
- distance(u, v): Find distance between two nodes - O(log n)
Space complexity: O(n log n)
Binary lifting allows us to "jump" up the tree in powers of 2, enabling
efficient LCA queries.
*/
import java.util.*;
class lca {
static class LCA {
private int n;
private int maxLog;
private Map<Integer, List<Integer>> graph;
private int[] depth;
private Map<Integer, Map<Integer, Integer>> up;
LCA(int n) {
this.n = n;
this.maxLog = (int) Math.ceil(Math.log(n) / Math.log(2)) + 1;
this.graph = new HashMap<>();
this.depth = new int[n];
this.up = new HashMap<>();
for (int i = 0; i < n; i++) {
graph.put(i, new ArrayList<>());
up.put(i, new HashMap<>());
}
}
void addEdge(int u, int v) {
graph.get(u).add(v);
graph.get(v).add(u);
}
void build(int root) {
Arrays.fill(depth, 0);
dfs(root, -1, 0);
}
private void dfs(int node, int parent, int d) {
depth[node] = d;
if (parent != -1) {
up.get(node).put(0, parent);
}
for (int i = 1; i < maxLog; i++) {
if (up.get(node).containsKey(i - 1)) {
int ancestor = up.get(node).get(i - 1);
if (up.get(ancestor).containsKey(i - 1)) {
up.get(node).put(i, up.get(ancestor).get(i - 1));
}
}
}
for (int child : graph.get(node)) {
if (child != parent) {
dfs(child, node, d + 1);
}
}
}
int query(int u, int v) {
if (depth[u] < depth[v]) {
int temp = u;
u = v;
v = temp;
}
// Bring u to the same level as v
int diff = depth[u] - depth[v];
for (int i = 0; i < maxLog; i++) {
if (((diff >> i) & 1) == 1) {
if (up.get(u).containsKey(i)) {
u = up.get(u).get(i);
}
}
}
if (u == v) {
return u;
}
// Binary search for LCA
for (int i = maxLog - 1; i >= 0; i--) {
if (up.get(u).containsKey(i) && up.get(v).containsKey(i)) {
int uAncestor = up.get(u).get(i);
int vAncestor = up.get(v).get(i);
if (uAncestor != vAncestor) {
u = uAncestor;
v = vAncestor;
}
}
}
return up.get(u).getOrDefault(0, u);
}
int distance(int u, int v) {
int lcaNode = query(u, v);
return depth[u] + depth[v] - 2 * depth[lcaNode];
}
}
static void testMain() {
LCA lca = new LCA(6);
lca.addEdge(0, 1); // 1-2
lca.addEdge(0, 2); // 1-3
lca.addEdge(1, 3); // 2-4
lca.addEdge(1, 4); // 2-5
lca.addEdge(2, 5); // 3-6
lca.build(0);
assert lca.query(3, 4) == 1; // LCA(4, 5) = 2
assert lca.query(3, 5) == 0; // LCA(4, 6) = 1
assert lca.distance(3, 5) == 4; // distance(4, 6) = 4
}
// Don't write tests below during competition.
static void testLinearTree() {
LCA lca = new LCA(5);
lca.addEdge(0, 1);
lca.addEdge(1, 2);
lca.addEdge(2, 3);
lca.addEdge(3, 4);
lca.build(0);
assert lca.query(0, 4) == 0;
assert lca.query(2, 4) == 2;
assert lca.distance(0, 4) == 4;
}
static void testSameNode() {
LCA lca = new LCA(3);
lca.addEdge(0, 1);
lca.addEdge(0, 2);
lca.build(0);
assert lca.query(1, 1) == 1;
assert lca.distance(1, 1) == 0;
}
static void testDeepTree() {
int n = 100;
LCA lca = new LCA(n);
for (int i = 0; i < n - 1; i++) {
lca.addEdge(i, i + 1);
}
lca.build(0);
assert lca.query(50, 99) == 50;
assert lca.distance(0, 99) == 99;
}
static void testComplexTree() {
LCA lca = new LCA(10);
lca.addEdge(0, 1);
lca.addEdge(0, 2);
lca.addEdge(1, 3);
lca.addEdge(1, 4);
lca.addEdge(2, 5);
lca.addEdge(3, 6);
lca.addEdge(3, 7);
lca.addEdge(4, 8);
lca.addEdge(5, 9);
lca.build(0);
assert lca.query(6, 7) == 3;
assert lca.query(6, 8) == 1;
assert lca.query(7, 9) == 0;
assert lca.distance(6, 7) == 2;
assert lca.distance(6, 9) == 6;
}
static void testBinaryTree() {
LCA lca = new LCA(7);
lca.addEdge(0, 1);
lca.addEdge(0, 2);
lca.addEdge(1, 3);
lca.addEdge(1, 4);
lca.addEdge(2, 5);
lca.addEdge(2, 6);
lca.build(0);
assert lca.query(3, 6) == 0;
assert lca.query(4, 5) == 0;
assert lca.distance(3, 6) == 4;
}
public static void main(String[] args) {
testLinearTree();
testSameNode();
testDeepTree();
testComplexTree();
testBinaryTree();
testMain();
System.out.println("All tests passed!");
}
}