-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLcaTemplate.java
More file actions
274 lines (244 loc) · 6.83 KB
/
Copy pathLcaTemplate.java
File metadata and controls
274 lines (244 loc) · 6.83 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package com.fqh;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
/**
* @author ikun
* @version v1.0.0
* @since 2024/1/24 15:09
**/
public class LcaTemplate {
//倍增求LCA模板
static final int N = (int) (5e5 + 10);
static int n, m, s;
static List<Integer>[] g = new List[N];
static int[] dep = new int[N];
static int[][] fa = new int[N][22];
static void dfs(int u, int father) { //树增dep,fa
dep[u] = dep[father] + 1;
// 向上跳1,2,4...步的祖先节点
fa[u][0] = father;
for (int i = 1; i <= 20; i++) {
fa[u][i] = fa[fa[u][i-1]][i-1];
}
for (int v : g[u]) {
if (v != father) {
dfs(v, u);
}
}
}
static int lca(int u, int v) {
if (dep[u] < dep[v]) {
// swap(u, v)
int temp = u;
u = v;
v = temp;
}
//u先大步后小步向上跳,直到与v同层
for (int i = 20; i >= 0; i--) {
if (dep[fa[u][i]] >= dep[v]) {
u = fa[u][i];
}
}
if (u == v) return v;
//u,v一起向上跳,直到lca的下面
for (int i = 20; i >= 0; i--) {
if (fa[u][i] != fa[v][i]) {
u = fa[u][i];
v = fa[v][i];
}
}
return fa[u][0];
}
public static void solve() throws IOException {
n = in.nextInt();
m = in.nextInt();
s = in.nextInt();
for (int i = 1; i <= n; i++) {
g[i] = new ArrayList<>();
}
for (int i = 1; i < n; i++) {
int x = in.nextInt();
int y = in.nextInt();
g[x].add(y);
g[y].add(x);
}
dfs(s, 0);
while (m-- > 0) {
int a = in.nextInt();
int b = in.nextInt();
out.println(lca(a, b));
}
}
public static void main(String[] args) throws Exception {
int T = 1;
while (T-- > 0) {
solve();
}
out.close();
}
static InputReader in = new InputReader();
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static class InputReader {
private StringTokenizer st;
private BufferedReader bf;
public InputReader() {
bf = new BufferedReader(new InputStreamReader(System.in));
st = null;
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public String nextLine() throws IOException {
return bf.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
}
class LCA {
//倍增求LCA模板
static final int N = (int) (5e5 + 10);
int n, s;
List<Integer>[] g = new List[N];
int[] dep = new int[N];
int[][] fa = new int[N][22];
/**
* dfs树上倍增
* @param u
* @param father
*/
public void dfs(int u, int father) { //树增dep,fa
dep[u] = dep[father] + 1;
// 向上跳1,2,4...步的祖先节点
fa[u][0] = father;
for (int i = 1; i <= 20; i++) {
fa[u][i] = fa[fa[u][i-1]][i-1];
}
for (int v : g[u]) {
if (v != father) {
dfs(v, u);
}
}
}
/**
* 返回u和v的最近公共祖先
* @param u
* @param v
* @return
*/
public int lca(int u, int v) {
if (dep[u] < dep[v]) {
// swap(u, v)
int temp = u;
u = v;
v = temp;
}
//u先大步后小步向上跳,直到与v同层
for (int i = 20; i >= 0; i--) {
if (dep[fa[u][i]] >= dep[v]) {
u = fa[u][i];
}
}
if (u == v) return v;
//u,v一起向上跳,直到lca的下面
for (int i = 20; i >= 0; i--) {
if (fa[u][i] != fa[v][i]) {
u = fa[u][i];
v = fa[v][i];
}
}
return fa[u][0];
}
/**
* 返回node的第k个祖先
* @param node
* @param k
* @return
*/
public int getKthAncestor(int node, int k) {
int m = 32 - Integer.numberOfLeadingZeros(k);
for (int i = 0; i < m; i++) {
if ((k >> i & 1) == 1) {
node = fa[node][i];
if (node == 0) // 跳出界了
break;
}
}
return node;
}
public LCA(int n) {
for (int i = 1; i <= n; i++) {
g[i] = new ArrayList<>();
}
}
}
//https://loj.ac/p/10130
//#10130. 「一本通 4.4 例 1」点的距离
class LOJ_10130 {
static LCA lca;
public static void solve() throws IOException {
int n = in.nextInt();
lca = new LCA(n);
for (int i = 0; i < n - 1; i++) {
int x = in.nextInt();
int y = in.nextInt();
lca.g[x].add(y);
lca.g[y].add(x);
}
lca.dfs(1, 0);
int q = in.nextInt();
while (q-- > 0) {
int x = in.nextInt();
int y = in.nextInt();
int LCA = lca.lca(x, y);
out.println(lca.dep[x] + lca.dep[y] - 2 * lca.dep[LCA]);
}
}
public static void main(String[] args) throws Exception {
int T = 1;
while (T-- > 0) {
solve();
}
out.close();
}
static InputReader in = new InputReader();
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static class InputReader {
private StringTokenizer st;
private BufferedReader bf;
public InputReader() {
bf = new BufferedReader(new InputStreamReader(System.in));
st = null;
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public String nextLine() throws IOException {
return bf.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
}