[Link] https://www.acmicpc.net/problem/3176
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
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader br;
static StringBuilder sb = new StringBuilder();
static int log = (int)(Math.log(100000) / Math.log(2));
static Node[] nodes;
static ArrayList<HashMap<Integer, Integer>> edges;
static int[][] parent;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
int n = toi(br.readLine());
int[] arr;
nodes = new Node[n];
for(int i = 0; i < n; i++) nodes[i] = new Node(i);
edges = new ArrayList<>();
for(int i = 0; i < n; i++) edges.add(new HashMap<Integer, Integer>());
for(int i = 0; i < n - 1; i++) {
arr = getArr();
int a = arr[0] - 1, b = arr[1] - 1, c = arr[2];
edges.get(a).put(b, c);
edges.get(b).put(a, c);
nodes[a].list.add(nodes[b]);
nodes[b].list.add(nodes[a]);
}
int[][] minArr = new int[n][log + 1];
int[][] maxArr = new int[n][log + 1];
parent = new int[n][log + 1];
for(int[] ar: minArr) Arrays.fill(ar, -1);
for(int[] ar: maxArr) Arrays.fill(ar, -1);
for(int[] ar: parent) Arrays.fill(ar, -1);
dfs(nodes[0], minArr, maxArr);
fillSperseTable(parent);
fillMax(maxArr, parent);
fillMin(minArr, parent);
int k = toi(br.readLine());
for(int i = 0; i < k; i++) {
arr = getArr();
int d = arr[0] - 1, e = arr[1] - 1;
int lca = lca(parent, d, e);
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int df1 = nodes[lca].depth - nodes[d].depth;
int df2 = nodes[lca].depth - nodes[e].depth;
for(int j = log; j >= 0; j--) {
if(nodes[d].depth - nodes[lca].depth >= 1 << j) {
min = Math.min(min, minArr[d][j]);
max = Math.max(max, maxArr[d][j]);
d = parent[d][j];
}
if(nodes[e].depth - nodes[lca].depth >= 1 << j) {
min = Math.min(min, minArr[e][j]);
max = Math.max(max, maxArr[e][j]);
e = parent[e][j];
}
}
sb.append(min).append(" ").append(max).append("\n");
}
print(sb);
}
static int lca(int[][] parent, int a, int b) {
if(nodes[a].depth < nodes[b].depth) {
int tmp = a;
a = b;
b = tmp;
}
for(int i = log; i >= 0; i--) {
if(nodes[a].depth - nodes[b].depth >= 1 << i) a = parent[a][i];
}
if(a == b) return a;
for(int i = log; i >= 0; i--) {
if(parent[a][i] != parent[b][i]) {
a = parent[a][i];
b = parent[b][i];
}
}
return parent[a][0];
}
static void fillMax(int[][] maxArr, int[][] parent) {
for(int j = 1; j <= log; j++) {
for(int i = 0; i < maxArr.length; i++) {
if(parent[i][j - 1] == -1 || parent[i][j] == -1) maxArr[i][j] = -1;
else maxArr[i][j] = Math.max(maxArr[i][j - 1], maxArr[parent[i][j - 1]][j - 1]);
}
}
}
static void fillMin(int[][] minArr, int[][] parent) {
for(int j = 1; j <= log; j++) {
for(int i = 0; i < minArr.length; i++) {
if(parent[i][j - 1] == -1 || parent[i][j] == -1) minArr[i][j] = Integer.MAX_VALUE;
else minArr[i][j] = Math.min(minArr[i][j - 1], minArr[parent[i][j - 1]][j - 1]);
}
}
}
static void fillSperseTable(int[][] sperse) {
for(int j = 1; j <= log; j++) {
for(int i = 0; i < sperse.length; i++) {
if(sperse[i][j - 1] == -1) continue;
sperse[i][j] = sperse[sperse[i][j - 1]][j - 1];
}
}
}
static void dfs(Node node, int[][] minArr, int[][] maxArr) {
for(Node child : node.list) {
if(node.parent == child.idx) continue;
minArr[child.idx][0] = maxArr[child.idx][0] = edges.get(node.idx).get(child.idx);
parent[child.idx][0] = node.idx;
child.depth = node.depth + 1;
child.parent = node.idx;
dfs(child, minArr, maxArr);
}
}
static class Node {
int idx;
int parent = -1;
int depth = 0;
ArrayList<Node> list = new ArrayList<>();
Node(int idx) { this.idx = idx; }
}
static int toi(String s) { return Integer.parseInt(s); }
static long tol(String s) { return Long.parseLong(s); }
static String[] getLine() throws IOException { return br.readLine().split(" "); }
static int[] getArr() throws IOException { return Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); }
static <T> void print(T s) { System.out.print(s); }
static <T> void println(T s) { System.out.println(s); }
}