結果
問題 | No.1094 木登り / Climbing tree |
ユーザー | tenten |
提出日時 | 2022-04-07 12:51:25 |
言語 | Java21 (openjdk 21) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 2,933 bytes |
コンパイル時間 | 2,775 ms |
コンパイル使用メモリ | 79,016 KB |
実行使用メモリ | 180,348 KB |
最終ジャッジ日時 | 2024-05-05 22:30:16 |
合計ジャッジ時間 | 30,646 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 54 ms
37,184 KB |
testcase_01 | AC | 1,984 ms
151,400 KB |
testcase_02 | AC | 1,226 ms
180,348 KB |
testcase_03 | AC | 428 ms
51,364 KB |
testcase_04 | AC | 983 ms
89,072 KB |
testcase_05 | AC | 1,865 ms
127,708 KB |
testcase_06 | AC | 1,133 ms
84,436 KB |
testcase_07 | TLE | - |
testcase_08 | TLE | - |
testcase_09 | TLE | - |
testcase_10 | TLE | - |
testcase_11 | TLE | - |
testcase_12 | TLE | - |
testcase_13 | TLE | - |
testcase_14 | TLE | - |
testcase_15 | AC | 974 ms
92,948 KB |
testcase_16 | AC | 1,664 ms
168,744 KB |
testcase_17 | AC | 1,340 ms
130,788 KB |
testcase_18 | AC | 1,364 ms
111,028 KB |
testcase_19 | AC | 1,797 ms
152,416 KB |
testcase_20 | TLE | - |
testcase_21 | AC | 1,410 ms
133,488 KB |
testcase_22 | TLE | - |
testcase_23 | TLE | - |
testcase_24 | TLE | - |
testcase_25 | TLE | - |
testcase_26 | TLE | - |
ソースコード
import java.io.*; import java.util.*; public class Main { static ArrayList<HashMap<Integer, Integer>> 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 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); } 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 (int x : graph.get(idx).keySet()) { if (x == p) { continue; } setDepth(x, idx, d + 1, c + 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 next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }