結果

問題 No.898 tri-βutree
ユーザー tentententen
提出日時 2023-07-07 18:01:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,456 ms / 4,000 ms
コード長 3,583 bytes
コンパイル時間 3,250 ms
コンパイル使用メモリ 85,368 KB
実行使用メモリ 125,764 KB
最終ジャッジ日時 2023-09-28 18:46:37
合計ジャッジ時間 31,786 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 782 ms
125,764 KB
testcase_01 AC 46 ms
49,376 KB
testcase_02 AC 59 ms
50,520 KB
testcase_03 AC 57 ms
50,392 KB
testcase_04 AC 55 ms
49,360 KB
testcase_05 AC 56 ms
50,248 KB
testcase_06 AC 57 ms
50,308 KB
testcase_07 AC 1,379 ms
119,316 KB
testcase_08 AC 1,371 ms
119,376 KB
testcase_09 AC 1,357 ms
121,548 KB
testcase_10 AC 1,420 ms
121,424 KB
testcase_11 AC 1,394 ms
121,852 KB
testcase_12 AC 1,399 ms
119,684 KB
testcase_13 AC 1,413 ms
119,736 KB
testcase_14 AC 1,406 ms
119,424 KB
testcase_15 AC 1,419 ms
122,196 KB
testcase_16 AC 1,456 ms
117,804 KB
testcase_17 AC 1,438 ms
117,560 KB
testcase_18 AC 1,426 ms
120,036 KB
testcase_19 AC 1,399 ms
119,144 KB
testcase_20 AC 1,412 ms
119,184 KB
testcase_21 AC 1,408 ms
119,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    static long[] values;
    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);
        }
        values = new long[n];
        parents = new int[17][n];
        depths = new int[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]];
            }
        }
        int q = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        while (q-- > 0) {
            int[] points = new int[]{sc.nextInt(), sc.nextInt(), sc.nextInt()};
            long ans = 0;
            for (int i = 0; i < 3; i++) {
                int lca = getLCA(points[i], points[(i + 1) % 3]);
                ans +=  values[points[i]] - values[lca];
            }
            sb.append(ans).append("\n");
        }
        System.out.print(sb);
    }
    
    static void setDepth(int idx, int p, int d, long v) {
        parents[0][idx] = p;
        depths[idx] = d;
        values[idx] = v;
        for (int x : graph.get(idx).keySet()) {
            if (x == p) {
                continue;
            }
            setDepth(x, idx, d + 1, v + graph.get(idx).get(x));
        }
    }
    
    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)) {
                continue;
            }
            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];
    }
}
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