結果

問題 No.1094 木登り / Climbing tree
ユーザー tentententen
提出日時 2021-03-12 08:29:12
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,340 bytes
コンパイル時間 4,849 ms
コンパイル使用メモリ 84,744 KB
実行使用メモリ 182,392 KB
最終ジャッジ日時 2024-04-25 19:40:07
合計ジャッジ時間 50,982 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
50,412 KB
testcase_01 AC 1,958 ms
166,304 KB
testcase_02 AC 1,069 ms
182,392 KB
testcase_03 AC 501 ms
68,680 KB
testcase_04 AC 933 ms
100,840 KB
testcase_05 AC 1,397 ms
135,592 KB
testcase_06 AC 1,090 ms
96,868 KB
testcase_07 AC 1,986 ms
171,388 KB
testcase_08 TLE -
testcase_09 AC 1,981 ms
173,300 KB
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 AC 1,980 ms
172,892 KB
testcase_14 TLE -
testcase_15 AC 925 ms
100,736 KB
testcase_16 AC 1,485 ms
169,568 KB
testcase_17 AC 1,126 ms
133,348 KB
testcase_18 AC 1,111 ms
113,440 KB
testcase_19 AC 1,209 ms
151,572 KB
testcase_20 TLE -
testcase_21 AC 1,228 ms
139,496 KB
testcase_22 AC 1,927 ms
175,536 KB
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    static int n;
    static int[][] parents;
    static int[] costs;
    static int[] depths;
	public static void main (String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		n = Integer.parseInt(br.readLine());
		for (int i = 0; i < n; i++) {
		    graph.add(new HashMap<>());
		}
		for (int i = 0; i < n - 1; i++) {
		    String[] line = br.readLine().split(" ", 3);
		    int a = Integer.parseInt(line[0]) - 1;
		    int b = Integer.parseInt(line[1]) - 1;
		    int c = Integer.parseInt(line[2]);
		    graph.get(a).put(b, c);
		    graph.get(b).put(a, c);
		}
		parents = new int[20][n];
		costs = new int[n];
		depths = new int[n];
        setDepth(0, 0, 0, 0);
        for (int i = 1; i < 20; i++) {
            for (int j = 0; j < n; j++) {
                parents[i][j] = parents[i - 1][parents[i - 1][j]];
            }
        }
        int q = Integer.parseInt(br.readLine());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < q; i++) {
		    String[] line = br.readLine().split(" ", 2);
		    int a = Integer.parseInt(line[0]) - 1;
		    int b = Integer.parseInt(line[1]) - 1;
            int lca = getLCA(a, b);
            sb.append(costs[a] - costs[lca] * 2 + costs[b]).append("\n");
        }
        System.out.print(sb);
   }
   
   static int getLCA(int a, int b) {
       if (depths[a] < depths[b]) {
           return getLCA(b, a);
       }
       for (int i = 19; i >= 0 && depths[a] > depths[b]; i--) {
           if (depths[a] - depths[b] >= (1 << i)) {
               a = parents[i][a];
           }
       }
       if (a == b) {
           return a;
       }
       for (int i = 19; i >= 0; i--) {
           if (parents[i][a] != parents[i][b]) {
               a = parents[i][a];
               b = parents[i][b];
           }
       }
       return parents[0][a];
   }
   
   static void setDepth(int idx, int p, int d, int c) {
       parents[0][idx] = p;
       costs[idx] = c;
       depths[idx] = d;
       for (int x : graph.get(idx).keySet()) {
           if (x == p) {
               continue;
           }
           setDepth(x, idx, d + 1, c + graph.get(idx).get(x));
       }
   }
}

0