-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathD.java
More file actions
148 lines (135 loc) · 4.86 KB
/
Copy pathD.java
File metadata and controls
148 lines (135 loc) · 4.86 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
package codeforces.tree_basics;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
public final class D {
public static void main(String[] args) {
final FastScanner fs = new FastScanner();
final PrintWriter pw = new PrintWriter(System.out);
final int n = fs.nextInt();
final int m = fs.nextInt();
final Map<Integer, List<int[]>> g = new HashMap<>();
for (int i = 0; i < m; i++) {
final int l = fs.nextInt() - 1;
final int r = fs.nextInt() - 1;
final int w = fs.nextInt();
g.computeIfAbsent(l, v -> new ArrayList<>()).add(new int[] { r, w });
g.computeIfAbsent(r, v -> new ArrayList<>()).add(new int[] { l, w });
}
final int[] parents = new int[n];
final int[] depth = new int[n];
final int[] costs = new int[n];
dfs(g, 0, -1, 0, parents, depth, costs);
final TreeAncestor ta = new TreeAncestor(n, parents, costs);
final int q = fs.nextInt();
for (int i = 0; i < q; i++) {
final int a = fs.nextInt() - 1;
final int b = fs.nextInt() - 1;
pw.println(ta.getMin(a, b, depth));
}
pw.flush();
}
private static void dfs(Map<Integer, List<int[]>> g, int u, int v, int d,
int[] parents,
int[] depth,
int[] costs) {
parents[u] = v;
depth[u] = d;
for (int[] next : g.getOrDefault(u, Collections.emptyList())) {
if (next[0] != v) {
costs[next[0]] = next[1];
dfs(g, next[0], u, d + 1, parents, depth, costs);
}
}
}
private static final class TreeAncestor {
int h;
int[][] parents;
int[][] minLift;
private TreeAncestor(int n, int[] parent, int[] costs) {
h = (int) (Math.log(n) / Math.log(2));
parents = new int[h + 1][n];
minLift = new int[h + 1][n];
minLift[0] = costs;
parents[0] = parent;
for (int i = 1; i <= h; i++) {
for (int j = 0; j < n; j++) {
final int nodeParent = parents[i - 1][j];
if (nodeParent != -1) {
parents[i][j] = parents[i - 1][nodeParent];
minLift[i][j] = Math.min(minLift[i - 1][j], minLift[i - 1][nodeParent]);
} else {
parents[i][j] = -1;
}
}
}
}
public int getKthAncestor(int node, int k, int[] ans) {
for (int i = 0; i <= h; i++) {
if ((k & (1 << i)) != 0) {
ans[0] = Math.min(ans[0], minLift[i][node]);
node = parents[i][node];
if (node == -1) {
return -1;
}
}
}
return node;
}
public int getMin(int u, int v, int[] depth) {
final int[] ans = { (int) 1e9 };
if (depth[u] < depth[v]) {
v = getKthAncestor(v, depth[v] - depth[u], ans);
} else {
u = getKthAncestor(u, depth[u] - depth[v], ans);
}
if (u == v) {
return ans[0];
}
for (int i = h; i >= 0; i--) {
if (parents[i][u] != parents[i][v]) {
ans[0] = Math.min(ans[0], minLift[i][u]);
ans[0] = Math.min(ans[0], minLift[i][v]);
u = parents[i][u];
v = parents[i][v];
}
}
ans[0] = Math.min(ans[0], minLift[0][u]);
ans[0] = Math.min(ans[0], minLift[0][v]);
return ans[0];
}
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
//noinspection CallToPrintStackTrace
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
final int[] a = new int[n];
for (int i = 0; i < n; i++) { a[i] = nextInt(); }
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}