|
| 1 | +package day1119; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +public class JY_1716 { |
| 7 | + |
| 8 | + static int N, M; |
| 9 | + static int[] d, parent, distance; |
| 10 | + static boolean[] c; |
| 11 | + static List<Node>[] g; |
| 12 | + static class Node { |
| 13 | + int n, dist; |
| 14 | + |
| 15 | + public Node(int n, int dist) { |
| 16 | + super(); |
| 17 | + this.n = n; |
| 18 | + this.dist = dist; |
| 19 | + } |
| 20 | + |
| 21 | + @Override |
| 22 | + public String toString() { |
| 23 | + return "Node [n=" + n + ", dist=" + dist + "]"; |
| 24 | + } |
| 25 | + |
| 26 | + } |
| 27 | + |
| 28 | + public static void main(String[] args) throws IOException{ |
| 29 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 30 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 31 | + |
| 32 | + N = Integer.parseInt(st.nextToken()); |
| 33 | + |
| 34 | + // ๋ถ๋ชจ ๋
ธ๋ ์ ๋ณด ๋ฐฐ์ด |
| 35 | + parent = new int[N+1]; |
| 36 | + |
| 37 | + // ๊น์ด ์ ๋ณด ๋ฐฐ์ด |
| 38 | + d = new int[N+1]; |
| 39 | + |
| 40 | + // ๊น์ด ๊ณ์ฐ ์ฌ๋ถ ๋ฐฐ์ด |
| 41 | + c = new boolean[N+1]; |
| 42 | + |
| 43 | + // ๋ฃจํธ๋
ธ๋๋ก ๋ถํฐ์ ๊ฑฐ๋ฆฌ |
| 44 | + distance = new int[N+1]; |
| 45 | + |
| 46 | + g = new ArrayList[N+1]; |
| 47 | + for(int i=0; i<N+1; i++) { |
| 48 | + g[i] = new ArrayList<>(); |
| 49 | + } |
| 50 | + |
| 51 | + for(int i=0; i<N-1; i++) { |
| 52 | + st = new StringTokenizer(br.readLine()); |
| 53 | + int a = Integer.parseInt(st.nextToken()); |
| 54 | + int b = Integer.parseInt(st.nextToken()); |
| 55 | + int dist = Integer.parseInt(st.nextToken()); |
| 56 | + g[a].add(new Node(b, dist)); |
| 57 | + g[b].add(new Node(a, dist)); |
| 58 | + } |
| 59 | + |
| 60 | + // ๊น์ด ๊ตฌํ๊ธฐ |
| 61 | + dfs(1, 0, 0); |
| 62 | + |
| 63 | + st = new StringTokenizer(br.readLine()); |
| 64 | + M = Integer.parseInt(st.nextToken()); |
| 65 | + |
| 66 | + StringBuilder sb = new StringBuilder(); |
| 67 | + for(int i=0; i<M; i++) { |
| 68 | + st = new StringTokenizer(br.readLine()); |
| 69 | + int n1 = Integer.parseInt(st.nextToken()); |
| 70 | + int n2 = Integer.parseInt(st.nextToken()); |
| 71 | + int p = lca(n1, n2); |
| 72 | + |
| 73 | + int ans = distance[n1]+distance[n2] - 2*distance[p]; |
| 74 | + |
| 75 | + sb.append(ans+"\n"); |
| 76 | + } |
| 77 | + |
| 78 | + System.out.println(sb.toString()); |
| 79 | + |
| 80 | + } |
| 81 | + public static void dfs(int x, int depth, int dist) { |
| 82 | + c[x] = true; |
| 83 | + d[x] = depth; |
| 84 | + distance[x] = dist; |
| 85 | + |
| 86 | + for(Node next: g[x]) { |
| 87 | + if(c[next.n]) continue; |
| 88 | + |
| 89 | + parent[next.n] = x; |
| 90 | + dfs(next.n, depth+1, dist+next.dist); |
| 91 | + } |
| 92 | + } |
| 93 | + public static int lca(int a, int b) { |
| 94 | + while(d[a] != d[b]) { |
| 95 | + if(d[a] > d[b]) { |
| 96 | + a = parent[a]; |
| 97 | + } else { |
| 98 | + b = parent[b]; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + while(a != b) { |
| 103 | + a = parent[a]; |
| 104 | + b = parent[b]; |
| 105 | + } |
| 106 | + return a; |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments