結果

問題 No.1094 木登り / Climbing tree
ユーザー tentententen
提出日時 2022-04-07 14:13:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,553 ms / 2,000 ms
コード長 3,123 bytes
コンパイル時間 3,239 ms
コンパイル使用メモリ 79,284 KB
実行使用メモリ 149,416 KB
最終ジャッジ日時 2024-04-25 19:46:04
合計ジャッジ時間 33,688 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
50,020 KB
testcase_01 AC 1,049 ms
125,520 KB
testcase_02 AC 929 ms
149,416 KB
testcase_03 AC 306 ms
61,156 KB
testcase_04 AC 524 ms
85,016 KB
testcase_05 AC 907 ms
102,540 KB
testcase_06 AC 611 ms
85,412 KB
testcase_07 AC 1,309 ms
135,256 KB
testcase_08 AC 1,143 ms
127,576 KB
testcase_09 AC 1,417 ms
126,588 KB
testcase_10 AC 1,399 ms
127,004 KB
testcase_11 AC 1,152 ms
125,092 KB
testcase_12 AC 1,365 ms
122,764 KB
testcase_13 AC 1,430 ms
137,120 KB
testcase_14 AC 1,512 ms
136,308 KB
testcase_15 AC 652 ms
93,776 KB
testcase_16 AC 1,005 ms
147,640 KB
testcase_17 AC 805 ms
107,100 KB
testcase_18 AC 718 ms
105,164 KB
testcase_19 AC 922 ms
122,788 KB
testcase_20 AC 1,553 ms
135,768 KB
testcase_21 AC 880 ms
118,708 KB
testcase_22 AC 1,156 ms
129,456 KB
testcase_23 AC 1,313 ms
122,936 KB
testcase_24 AC 1,374 ms
137,188 KB
testcase_25 AC 1,347 ms
124,000 KB
testcase_26 AC 1,015 ms
123,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<ArrayList<Path>> graph = new ArrayList<>();
    static int[] depth;
    static int[][] parents;
    static int[] costs;
    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 ArrayList<>());
        }
        for (int i = 0; i < n - 1; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            int c = sc.nextInt();
            graph.get(a).add(new Path(b, c));
            graph.get(b).add(new Path(a, c));
        }
        depth = new int[n];
        parents = new int[18][n];
        costs = new int[n];
        setDepth(0, 0, 0, 0);
        for (int i = 1; i < 18; 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() - 1;
            int b = sc.nextInt() - 1;
            int lca = getLCA(a, b);
            sb.append(costs[a] + costs[b] - costs[lca] * 2).append("\n");
        }
        System.out.print(sb);
    }
    
    static int getLCA(int left, int right) {
        if (depth[left] < depth[right]) {
            return getLCA(right, left);
        }
        for (int i = 17; i >= 0; i--) {
            if (depth[left] - depth[right] >= (1 << i)) {
                left = parents[i][left];
            }
        }
        if (left == right) {
            return left;
        }
        for (int i = 17; i >= 0; i--) {
            if (parents[i][left] != parents[i][right]) {
                left = parents[i][left];
                right = parents[i][right];
            }
        }
        return parents[0][left];
    }
    
    static void setDepth(int idx, int p, int d, int c) {
        depth[idx] = d;
        parents[0][idx] = p;
        costs[idx] = c;
        for (Path x : graph.get(idx)) {
            if (x.idx == p) {
                continue;
            }
            setDepth(x.idx, idx, d + 1, c + x.value);
        }
    }
    
    static class Path {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
    }
}
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 {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0