|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.StringTokenizer; |
| 7 | + |
| 8 | +public class SB_1716 { |
| 9 | + static int N, M; |
| 10 | + static List<List<Node>> adj = new ArrayList<>(); |
| 11 | + static int[][] parents; |
| 12 | + static int mxDepth; |
| 13 | + static int[] depths; |
| 14 | + static int[] dist; // ๊ฐ ์ ์ ์์ ๋ฃจํธ๋ก ๋ถํฐ ๋จ์ด์ง ๊ฑฐ๋ฆฌ |
| 15 | + |
| 16 | + private static int LCA(int a, int b) { // ๊ณตํต ์กฐ์ ๊ตฌํ๊ธฐ |
| 17 | + if (depths[a] < depths[b]) { // a์ ๊น์ด๊ฐ ๊น๋๋ก ์ค์ |
| 18 | + int tmp = b; |
| 19 | + b = a; |
| 20 | + a = tmp; |
| 21 | + } |
| 22 | + |
| 23 | + for (int i = mxDepth - 1; i >= 0; i--) { // ๊น์ด ๋์ผํ๊ธฐ ๋ง์ถ๊ธฐ |
| 24 | + if (depths[a] - depths[b] >= (1 << i)) { // ๊น์ด ์ฐจ๊ฐ 2์ ๊ณฑ๊ผด๋งํผ ํฌ๋ค๋ฉด |
| 25 | + a = parents[a][i]; // ๋ ๊น์ ๋
ธ๋๋ฅผ i๋งํผ ์ ํ |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + if (a==b) return a; // ๊น์ด ์กฐ์ ํ์๋ ๋ฐ๋ก ๋ถ๋ชจ๊ฐ ๊ฐ์ผ๋ฉด ๋ |
| 30 | + |
| 31 | + for (int i = mxDepth - 1; i >= 0; i--) { // ๋ถ๋ชจ๊ฐ ๋ค๋ฅด๋ฉด ๊ฐ์ด ๋ถ๋ชจ์ฐพ์ผ๋ฌ ๋ ๋๊ธฐ |
| 32 | + if (parents[a][i] != parents[b][i]) { |
| 33 | + a = parents[a][i]; |
| 34 | + b = parents[b][i]; |
| 35 | + } |
| 36 | + } |
| 37 | + return parents[a][0]; // ์ต์ข
์ ์ผ๋ก ์ฐพ์ ๋ถ๋ชจ ๊ฐ ๋ฐํ |
| 38 | + } |
| 39 | + private static void setTree(int depth, int x, int p, int cost) { |
| 40 | + depths[x] = depth; |
| 41 | + dist[x] = cost; // ๋ฃจํธ์์ ํ์ฌ ๋
ธ๋x๊น์ง์ ๊ฑฐ๋ฆฌ |
| 42 | + for (Node nxt : adj.get(x)) { |
| 43 | + if (nxt.u==p) continue; // ๋ค์ ๋
ธ๋๊ฐ ๋ถ๋ชจ๋
ธ๋๋ฅผ ๊ฐ๋ฅดํค๋ฉด ํจ์ฐ |
| 44 | + parents[nxt.u][0] = x; // ๋ค์ ๋
ธ๋์ ๋ฐ๋ก ์ ๋ถ๋ชจ๋ฅผ ์์ ์ผ๋ก ์ค์ |
| 45 | + setTree(depth + 1, nxt.u, x, cost + nxt.cost); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private static void setParents() { // 2์ ๊ณฑ๋ณ ๋ถ๋ชจ๊ฐ ๊ตฌํ๊ธฐ, setTree์์ ๊ตฌํ ๋ฐ๋ก ์ง์ ๋ถ๋ชจ๊ฐ์ ๋ฐํ์ผ๋ก 2^i๋ฒ์งธ ๋ถ๋ชจ ๊ฐ ๊ตฌํ๊ธฐ |
| 50 | + for (int i = 1; i <= mxDepth; i++) { |
| 51 | + for (int j = 1; j < N + 1; j++) { |
| 52 | + parents[j][i] = parents[parents[j][i - 1]][i - 1]; // j๋
ธ๋์ 2^i๋ฒ์งธ ๊ฐ์, 2^i์ ์กฐ์์ ์กฐ์ ๊ฐ |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + public static void main(String[] args) throws IOException { |
| 57 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 58 | + StringTokenizer st; |
| 59 | + StringBuilder sb = new StringBuilder(); |
| 60 | + N = Integer.parseInt(br.readLine()); |
| 61 | + |
| 62 | + depths = new int[N + 1]; |
| 63 | + dist = new int[N + 1]; |
| 64 | + mxDepth = (int)Math.ceil(Math.log(N) / Math.log(2)) + 1; // ์ต๋๋์ด ๊ตฌํ๊ธฐ |
| 65 | + parents = new int[N + 1][mxDepth+1]; |
| 66 | + for (int i = 0; i < N + 1; i++) { |
| 67 | + adj.add(new ArrayList<>()); |
| 68 | + } |
| 69 | + for (int i = 0; i < N - 1; i++) { |
| 70 | + st = new StringTokenizer(br.readLine()); |
| 71 | + int u = Integer.parseInt(st.nextToken()); |
| 72 | + int v = Integer.parseInt(st.nextToken()); |
| 73 | + int c = Integer.parseInt(st.nextToken()); |
| 74 | + adj.get(u).add(new Node(v, c)); |
| 75 | + adj.get(v).add(new Node(u, c)); |
| 76 | + } |
| 77 | + |
| 78 | + // ํธ๋ฆฌ ๊ตฌ์ฑํ๊ธฐ |
| 79 | + setTree(1, 1, 0, 0); // 1๋ฒ๋
ธ๋๋ฅผ ๋ฃจํธ๋ก ๊น์ด1๋ถํฐ ์์ |
| 80 | + setParents(); |
| 81 | + |
| 82 | + M = Integer.parseInt(br.readLine()); |
| 83 | + while (M-- > 0) { |
| 84 | + st = new StringTokenizer(br.readLine()); |
| 85 | + int a = Integer.parseInt(st.nextToken()); |
| 86 | + int b = Integer.parseInt(st.nextToken()); |
| 87 | + sb.append(dist[a] + dist[b] - (dist[LCA(a, b)] * 2)).append('\n'); |
| 88 | + } |
| 89 | + System.out.println(sb); |
| 90 | + } |
| 91 | + static class Node{ |
| 92 | + int u, cost; |
| 93 | + public Node(int u, int cost) { |
| 94 | + this.u = u; |
| 95 | + this.cost = cost; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments