結果

問題 No.898 tri-βutree
ユーザー tentententen
提出日時 2020-10-20 17:50:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,958 ms / 4,000 ms
コード長 2,486 bytes
コンパイル時間 2,575 ms
コンパイル使用メモリ 79,744 KB
実行使用メモリ 143,244 KB
最終ジャッジ日時 2024-04-26 09:33:22
合計ジャッジ時間 35,932 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,388 ms
143,244 KB
testcase_01 AC 125 ms
41,424 KB
testcase_02 AC 176 ms
42,304 KB
testcase_03 AC 161 ms
42,216 KB
testcase_04 AC 161 ms
42,168 KB
testcase_05 AC 155 ms
42,232 KB
testcase_06 AC 161 ms
42,380 KB
testcase_07 AC 1,897 ms
122,552 KB
testcase_08 AC 1,894 ms
122,628 KB
testcase_09 AC 1,918 ms
121,860 KB
testcase_10 AC 1,953 ms
121,936 KB
testcase_11 AC 1,922 ms
125,692 KB
testcase_12 AC 1,941 ms
121,968 KB
testcase_13 AC 1,914 ms
122,352 KB
testcase_14 AC 1,958 ms
122,524 KB
testcase_15 AC 1,830 ms
122,452 KB
testcase_16 AC 1,825 ms
121,872 KB
testcase_17 AC 1,911 ms
122,376 KB
testcase_18 AC 1,873 ms
122,576 KB
testcase_19 AC 1,898 ms
121,912 KB
testcase_20 AC 1,787 ms
121,800 KB
testcase_21 AC 1,836 ms
122,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static int[][] parents;
    static int[] depth;
    static long[] weights;
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        parents  = new int[30][n];
        depth = new int[n];
        weights = new long[n];
        for (int i = 0; i < n; i++) {
            graph.add(new HashMap<>());
        }
        for (int i = 0; i < n - 1; i ++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int w = sc.nextInt();
            graph.get(a).put(b, w);
            graph.get(b).put(a, w);
        }
        setDepth(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 a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            int cc = getLCA(a, b);
            int aa = getLCA(b, c);
            int bb = getLCA(c, a);
            long ans = weights[a] + weights[b] + weights[c] - weights[aa] - weights[bb] - weights[cc];
            sb.append(ans).append("\n");
        }
        System.out.print(sb);
    }
    
    static int getLCA(int a, int b) {
        if (depth[a] < depth[b]) {
            return getLCA(b, a);
        }
        int diff = depth[a] - depth[b];
        for (int i = 29; i >= 0 && diff > 0; i--) {
            if (diff < (1 << i)) {
                continue;
            }
            a = parents[i][a];
            diff = depth[a] - depth[b];
        }
        if (a == b) {
            return a;
        }
        for (int i = 29; 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 parent, int d, long w) {
        parents[0][idx] = parent;
        depth[idx] = d;
        weights[idx] = w;
        for (Map.Entry<Integer, Integer> entry : graph.get(idx).entrySet()) {
            if (entry.getKey() == parent) {
                continue;
            }
            setDepth(entry.getKey(), idx, d + 1, w + entry.getValue());
        }
    }
} 
0