結果

問題 No.898 tri-βutree
ユーザー tentententen
提出日時 2022-08-12 15:13:12
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,291 ms / 4,000 ms
コード長 3,101 bytes
コンパイル時間 3,488 ms
コンパイル使用メモリ 80,156 KB
実行使用メモリ 136,996 KB
最終ジャッジ日時 2023-10-24 00:42:42
合計ジャッジ時間 29,945 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 964 ms
136,996 KB
testcase_01 AC 52 ms
53,536 KB
testcase_02 AC 58 ms
53,568 KB
testcase_03 AC 59 ms
53,560 KB
testcase_04 AC 58 ms
53,572 KB
testcase_05 AC 59 ms
53,568 KB
testcase_06 AC 60 ms
52,600 KB
testcase_07 AC 1,263 ms
124,480 KB
testcase_08 AC 1,289 ms
124,412 KB
testcase_09 AC 1,269 ms
124,360 KB
testcase_10 AC 1,259 ms
124,208 KB
testcase_11 AC 1,238 ms
124,324 KB
testcase_12 AC 1,230 ms
124,388 KB
testcase_13 AC 1,232 ms
118,552 KB
testcase_14 AC 1,291 ms
124,504 KB
testcase_15 AC 1,273 ms
124,320 KB
testcase_16 AC 1,230 ms
124,416 KB
testcase_17 AC 1,201 ms
118,788 KB
testcase_18 AC 1,251 ms
124,408 KB
testcase_19 AC 1,233 ms
118,468 KB
testcase_20 AC 1,254 ms
124,412 KB
testcase_21 AC 1,244 ms
124,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    static long[] weights;
    static int[][] parents;
    static int[] depths;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        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();
            int b = sc.nextInt();
            int c = sc.nextInt();
            graph.get(a).put(b, c);
            graph.get(b).put(a, c);
        }
        weights = new long[n];
        depths = new int[n];
        parents = new int[17][n];
        setDepth(0, 0, 0, 0);
        for (int i = 1; i < 17; i++) {
            for (int j = 0; j < n; j++) {
                parents[i][j] = parents[i - 1][parents[i - 1][j]];
            }
        }
        StringBuilder sb = new StringBuilder();
        int q = sc.nextInt();
        while (q-- > 0) {
            int[] targets  = new int[]{sc.nextInt(), sc.nextInt(), sc.nextInt()};
            long sum = 0;
            for (int i = 0; i < 3; i++) {
                sum += getWeight(targets[i], targets[(i + 1) % 3]);
            }
            sb.append(sum / 2).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 = 16; i >= 0; i--) {
            if (depths[a] - depths[b] >= (1 << i)) {
                a = parents[i][a];
            }
        }
        if (a == b) {
            return a;
        }
        for (int i = 16; i >= 0; i--) {
            if (parents[i][a] != parents[i][b]) {
                a = parents[i][a];
                b = parents[i][b];
            }
        }
        return parents[0][a];
    }
    
    static long getWeight(int a, int b) {
        int lca = getLCA(a, b);
        return weights[a] + weights[b] - weights[lca] * 2;
    }
    
    static void setDepth(int idx, int p, long w, int d) {
        depths[idx] = d;
        weights[idx] = w;
        parents[0][idx] = p;
        for (int x : graph.get(idx).keySet()) {
            if (x == p) {
                continue;
            }
            setDepth(x, idx, w + graph.get(idx).get(x), d + 1);
        }
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0