結果

問題 No.1094 木登り / Climbing tree
ユーザー tentententen
提出日時 2020-12-03 16:31:57
言語 Java19
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,632 bytes
コンパイル時間 3,549 ms
コンパイル使用メモリ 75,032 KB
実行使用メモリ 185,692 KB
最終ジャッジ日時 2023-08-08 01:34:08
合計ジャッジ時間 49,403 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,460 KB
testcase_01 AC 1,882 ms
178,496 KB
testcase_02 AC 1,006 ms
185,448 KB
testcase_03 AC 501 ms
66,612 KB
testcase_04 AC 931 ms
105,864 KB
testcase_05 AC 1,505 ms
147,484 KB
testcase_06 AC 1,082 ms
97,288 KB
testcase_07 AC 1,894 ms
183,436 KB
testcase_08 AC 1,940 ms
185,484 KB
testcase_09 AC 1,943 ms
185,692 KB
testcase_10 AC 1,959 ms
183,496 KB
testcase_11 AC 1,932 ms
183,420 KB
testcase_12 AC 1,979 ms
184,176 KB
testcase_13 AC 1,944 ms
185,308 KB
testcase_14 AC 1,906 ms
183,264 KB
testcase_15 AC 888 ms
99,980 KB
testcase_16 AC 1,823 ms
175,492 KB
testcase_17 AC 1,135 ms
134,680 KB
testcase_18 AC 984 ms
121,300 KB
testcase_19 AC 1,261 ms
168,956 KB
testcase_20 AC 1,901 ms
185,476 KB
testcase_21 AC 1,298 ms
149,260 KB
testcase_22 TLE -
testcase_23 AC 1,796 ms
172,664 KB
testcase_24 AC 1,737 ms
174,020 KB
testcase_25 AC 1,808 ms
172,760 KB
testcase_26 AC 1,829 ms
173,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int 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);
        }
        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 = Integer.parseInt(br.readLine());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < q; i++) {
            String[] line = br.readLine().split(" ", 2);
            int s = Integer.parseInt(line[0]) - 1;
            int t = Integer.parseInt(line[1]) - 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