結果

問題 No.898 tri-βutree
ユーザー tentententen
提出日時 2023-11-16 11:39:51
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,608 ms / 4,000 ms
コード長 3,559 bytes
コンパイル時間 3,173 ms
コンパイル使用メモリ 87,460 KB
実行使用メモリ 131,188 KB
最終ジャッジ日時 2023-11-16 11:40:24
合計ジャッジ時間 33,037 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 876 ms
131,188 KB
testcase_01 AC 56 ms
53,984 KB
testcase_02 AC 65 ms
54,128 KB
testcase_03 AC 64 ms
54,116 KB
testcase_04 AC 64 ms
54,008 KB
testcase_05 AC 63 ms
54,108 KB
testcase_06 AC 64 ms
54,000 KB
testcase_07 AC 1,608 ms
118,844 KB
testcase_08 AC 1,391 ms
119,048 KB
testcase_09 AC 1,569 ms
118,836 KB
testcase_10 AC 1,412 ms
117,256 KB
testcase_11 AC 1,564 ms
118,948 KB
testcase_12 AC 1,383 ms
119,284 KB
testcase_13 AC 1,396 ms
118,896 KB
testcase_14 AC 1,458 ms
118,908 KB
testcase_15 AC 1,589 ms
119,020 KB
testcase_16 AC 1,446 ms
119,004 KB
testcase_17 AC 1,420 ms
118,948 KB
testcase_18 AC 1,583 ms
118,808 KB
testcase_19 AC 1,489 ms
118,836 KB
testcase_20 AC 1,396 ms
118,860 KB
testcase_21 AC 1,483 ms
119,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    static int[][] parents;
    static int[] depths;
    static long[] values;
    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);
        }
        parents = new int[20][n];
        depths = new int[n];
        values = new long[n];
        setDepths(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 = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        while (q-- > 0) {
            sb.append(getValue(new int[]{sc.nextInt(), sc.nextInt(), sc.nextInt()})).append("\n");
        }
        System.out.print(sb);
    }
    
    static void setDepths(int idx, int p, int d, long v) {
        depths[idx] = d;
        parents[0][idx] = p;
        values[idx] = v;
        for (int x : graph.get(idx).keySet()) {
            if (x != p) {
                setDepths(x, idx, d + 1, v + graph.get(idx).get(x));
            }
        }
    }
    
    static long getValue(int[] idxes) {
        long ans = 0;
        for (int i = 0; i < 3; i++) {
            ans += values[idxes[i]];
            ans -= values[getLCA(idxes[i], idxes[(i + 1) % 3])];
        }
        return ans;
    }
    
    static int getLCA(int a, int b) {
        if (depths[a] < depths[b]) {
            return getLCA(b, a);
        }
        for (int i = 19; i >= 0; 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];
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0