結果

問題 No.1094 木登り / Climbing tree
ユーザー tentententen
提出日時 2023-03-17 12:49:03
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,408 bytes
コンパイル時間 4,792 ms
コンパイル使用メモリ 94,868 KB
実行使用メモリ 194,316 KB
最終ジャッジ日時 2023-10-18 13:36:20
合計ジャッジ時間 53,373 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
53,580 KB
testcase_01 AC 1,875 ms
166,812 KB
testcase_02 AC 1,102 ms
194,316 KB
testcase_03 AC 411 ms
64,312 KB
testcase_04 AC 879 ms
102,140 KB
testcase_05 AC 1,373 ms
133,852 KB
testcase_06 AC 989 ms
98,800 KB
testcase_07 AC 1,990 ms
176,796 KB
testcase_08 AC 1,882 ms
176,840 KB
testcase_09 TLE -
testcase_10 AC 1,968 ms
176,912 KB
testcase_11 AC 1,924 ms
174,872 KB
testcase_12 TLE -
testcase_13 AC 1,990 ms
174,568 KB
testcase_14 AC 1,954 ms
176,072 KB
testcase_15 AC 778 ms
101,688 KB
testcase_16 AC 1,311 ms
176,428 KB
testcase_17 AC 1,148 ms
138,004 KB
testcase_18 AC 969 ms
117,612 KB
testcase_19 AC 1,273 ms
159,480 KB
testcase_20 TLE -
testcase_21 AC 1,174 ms
139,804 KB
testcase_22 AC 1,855 ms
176,660 KB
testcase_23 AC 1,888 ms
165,452 KB
testcase_24 AC 1,789 ms
169,380 KB
testcase_25 AC 1,809 ms
166,776 KB
testcase_26 AC 1,843 ms
165,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int[] values;
    static int[] depths;
    static int[][] parents;
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    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() - 1;
            int b = sc.nextInt() - 1;
            int c = sc.nextInt();
            graph.get(a).put(b, c);
            graph.get(b).put(a, c);
        }
        values = new int[n];
        depths = new int[n];
        parents = new int[18][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();
        while (q-- > 0) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            int lca = getLCA(a, b);
            sb.append(values[a] + values[b] - values[lca] * 2).append("\n");
        }
        System.out.print(sb);
    }
    
    static void setDepth(int idx, int p, int d, int v) {
        parents[0][idx] = p;
        depths[idx] = d;
        values[idx] = v;
        for (int x : graph.get(idx).keySet()) {
            if (p != x) {
                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 = 17; i >= 0; i--) {
            if (depths[a] - depths[b] >= (1 << i)) {
                a = parents[i][a];
            }
        }
        if (a == b) {
            return a;
        }
        for (int i = 17; 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