結果

問題 No.898 tri-βutree
ユーザー tentententen
提出日時 2021-11-04 10:20:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,545 ms / 4,000 ms
コード長 3,134 bytes
コンパイル時間 2,723 ms
コンパイル使用メモリ 80,736 KB
実行使用メモリ 126,532 KB
最終ジャッジ日時 2024-04-22 08:18:04
合計ジャッジ時間 29,390 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 895 ms
126,532 KB
testcase_01 AC 54 ms
48,392 KB
testcase_02 AC 60 ms
37,300 KB
testcase_03 AC 59 ms
37,192 KB
testcase_04 AC 60 ms
37,156 KB
testcase_05 AC 60 ms
37,348 KB
testcase_06 AC 61 ms
37,460 KB
testcase_07 AC 1,414 ms
112,992 KB
testcase_08 AC 1,545 ms
110,864 KB
testcase_09 AC 1,381 ms
108,388 KB
testcase_10 AC 1,355 ms
109,328 KB
testcase_11 AC 1,393 ms
113,456 KB
testcase_12 AC 1,362 ms
109,640 KB
testcase_13 AC 1,344 ms
112,772 KB
testcase_14 AC 1,395 ms
106,012 KB
testcase_15 AC 1,519 ms
110,100 KB
testcase_16 AC 1,383 ms
106,064 KB
testcase_17 AC 1,456 ms
112,780 KB
testcase_18 AC 1,436 ms
112,876 KB
testcase_19 AC 1,436 ms
122,568 KB
testcase_20 AC 1,433 ms
121,828 KB
testcase_21 AC 1,340 ms
120,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    static int[][] parents;
    static int[] depths;
    static long[] weights;
    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[17][n];
        depths = new int[n];
        weights = new long[n];
        getWeight(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();
        for (int i = 0; i < q; i++) {
            sb.append(getAns(sc.nextInt(), sc.nextInt(), sc.nextInt())).append("\n");
        }
        System.out.print(sb);
    }
    
    static long getEach(int a, int b) {
        return weights[a] + weights[b] - weights[getLCA(a, b)] * 2;
    }
    
    static int getLCA(int a, int b) {
        if (depths[a] < depths[b]) {
            return getLCA(b, a);
        }
        for (int i = 16; i >= 0 && depths[a] > depths[b]; 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 getAns(int a, int b, int c) {
        return (getEach(a, b) + getEach(b, c) + getEach(c, a)) / 2;
    }
    
    static void getWeight(int idx, int p, int d, long w) {
        parents[0][idx] = p;
        depths[idx] = d;
        weights[idx] = w;
        for (int x : graph.get(idx).keySet()) {
            if (x == p) {
                continue;
            }
            getWeight(x, idx, d + 1, w + graph.get(idx).get(x));
        }
    }
}
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 nextLine() throws Exception {
        return br.readLine();
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0