結果

問題 No.1094 木登り / Climbing tree
ユーザー tentententen
提出日時 2020-12-03 16:28:06
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,343 bytes
コンパイル時間 6,621 ms
コンパイル使用メモリ 79,316 KB
実行使用メモリ 191,384 KB
最終ジャッジ日時 2023-10-12 02:22:38
合計ジャッジ時間 12,285 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
60,104 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static int n;
    static int[] depths;
    static int[][] parents;
    static int[] costs;
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            graph.add(new HashMap<>());
        }
        for (int i = 0; i < n - 1; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            int c = sc.nextInt();
            graph.get(a).put(b, c);
            graph.get(b).put(a, c);
        }
        depths = new int[n];
        parents = new int[30][n];
        costs = new int[n];
        setValue(0, 0, 0, 0);
        for (int i = 1; i < 30; i++) {
            for (int j = 0; j < n; j++) {
                parents[i][j] = parents[i - 1][parents[i - 1][j]];
            }
        }
        int q = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < q; i++) {
            int s = sc.nextInt() - 1;
            int t = sc.nextInt() - 1;
            int lca = getLCA(s, t);
            long ans = (long)costs[s] - (long)costs[lca] * 2 + (long)costs[t];
            sb.append(ans).append("\n");
        }
        System.out.print(sb);
    }
    
    static int getLCA(int x, int y) {
        if (depths[x] < depths[y]) {
            return getLCA(y, x);
        }
        int def = depths[x] - depths[y];
        for (int i = 29; i >= 0 && depths[x] > depths[y]; i--) {
            if (def >= (1 << i)) {
                def -=(1 << i);
                x = parents[i][x];
            }
        }
        for (int i = 29; i >= 0; i--) {
            if (parents[i][x] == parents[i][y]) {
                continue;
            }
            x = parents[i][x];
            y = parents[i][y];
        }
        if (x != y) {
            return parents[0][x];
        } else {
            return x;
        }
    }
    
    static void setValue(int idx, int p, int d, int c) {
        depths[idx] = d;
        parents[0][idx] = p;
        costs[idx] = c;
        for (int x : graph.get(idx).keySet()) {
            if (x == p) {
                continue;
            }
            setValue(x, idx, d + 1, c + graph.get(idx).get(x));
        }
    }
}
0