|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +/* |
| 5 | + * 정점들의 거리 |
| 6 | + */ |
| 7 | + |
| 8 | +public class DH_1761 { |
| 9 | + static class Node { |
| 10 | + int e, w; |
| 11 | + public Node(int e, int w) { |
| 12 | + this.e = e; |
| 13 | + this.w = w; |
| 14 | + } |
| 15 | + } |
| 16 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 17 | + static StringBuilder sb = new StringBuilder(); |
| 18 | + static StringTokenizer st; |
| 19 | + static int N; |
| 20 | + static ArrayList<Node> adj[]; |
| 21 | + static int[] depth, p, dis; |
| 22 | + |
| 23 | + public static void main(String[] args) throws Exception { |
| 24 | + initInput(); |
| 25 | + solution(); |
| 26 | + } |
| 27 | + |
| 28 | + static void solution() throws Exception { |
| 29 | + bfs(); |
| 30 | + getDis(); |
| 31 | + } |
| 32 | + |
| 33 | + static void getDis() throws Exception { |
| 34 | + int M = Integer.parseInt(br.readLine()); |
| 35 | + |
| 36 | + for(int i = 0; i < M; i++) { |
| 37 | + st = new StringTokenizer(br.readLine()); |
| 38 | + |
| 39 | + int a = Integer.parseInt(st.nextToken()); |
| 40 | + int b = Integer.parseInt(st.nextToken()); |
| 41 | + |
| 42 | + // a, b의 최소공통조상 구하고, 두 점 간 사이 구하기 |
| 43 | + sb.append(LCA(a, b)).append("\n"); |
| 44 | + } |
| 45 | + |
| 46 | + System.out.print(sb); |
| 47 | + } |
| 48 | + |
| 49 | + static long LCA(int a, int b) { |
| 50 | + // result = (루트 ~ a) + (루트 ~ b) - 2 * (루트 ~ 최소공통조상) |
| 51 | + long result = dis[a] + dis[b]; |
| 52 | + |
| 53 | + int depthA = depth[a]; |
| 54 | + int depthB = depth[b]; |
| 55 | + |
| 56 | + // 무조건 a가 depth가 작도록 |
| 57 | + if(depthB < depthA) { |
| 58 | + int tmp = a; |
| 59 | + a = b; |
| 60 | + b = tmp; |
| 61 | + } |
| 62 | + |
| 63 | + int diffDis = Math.abs(depthA - depthB); |
| 64 | + |
| 65 | + // 깊이 맞추기 |
| 66 | + for(int i = 0; i < diffDis; i++) b = p[b]; |
| 67 | + |
| 68 | + while(a != b) { |
| 69 | + a = p[a]; |
| 70 | + b = p[b]; |
| 71 | + } |
| 72 | + |
| 73 | + return result - 2 * dis[a]; |
| 74 | + } |
| 75 | + |
| 76 | + static void bfs() { |
| 77 | + boolean[] v = new boolean[N + 1]; |
| 78 | + |
| 79 | + Queue<Node> q = new ArrayDeque<>(); |
| 80 | + q.add(new Node(1, 0)); |
| 81 | + v[1] = true; |
| 82 | + |
| 83 | + while(!q.isEmpty()) { |
| 84 | + Node current = q.poll(); |
| 85 | + |
| 86 | + for(Node next: adj[current.e]) { |
| 87 | + if(v[next.e]) continue; |
| 88 | + p[next.e] = current.e; |
| 89 | + depth[next.e] = depth[current.e] + 1; |
| 90 | + dis[next.e] = dis[current.e] + next.w; |
| 91 | + v[next.e] = true; |
| 92 | + q.add(next); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + static void initInput() throws Exception { |
| 99 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 100 | + N = Integer.parseInt(br.readLine()); |
| 101 | + |
| 102 | + adj = new ArrayList[N + 1]; |
| 103 | + depth = new int[N + 1]; |
| 104 | + dis = new int[N + 1]; |
| 105 | + p = new int[N + 1]; |
| 106 | + |
| 107 | + for(int i = 0; i < adj.length; i++) adj[i] = new ArrayList<>(); |
| 108 | + |
| 109 | + for(int i = 0; i < N - 1; i++) { |
| 110 | + st = new StringTokenizer(br.readLine()); |
| 111 | + |
| 112 | + int s = Integer.parseInt(st.nextToken()); |
| 113 | + int e = Integer.parseInt(st.nextToken()); |
| 114 | + int w = Integer.parseInt(st.nextToken()); |
| 115 | + adj[s].add(new Node(e, w)); |
| 116 | + adj[e].add(new Node(s, w)); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments