結果
問題 | No.898 tri-βutree |
ユーザー | tenten |
提出日時 | 2024-07-22 13:41:54 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,445 ms / 4,000 ms |
コード長 | 3,302 bytes |
コンパイル時間 | 3,181 ms |
コンパイル使用メモリ | 83,424 KB |
実行使用メモリ | 119,620 KB |
最終ジャッジ日時 | 2024-07-22 13:42:34 |
合計ジャッジ時間 | 27,378 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 852 ms
119,620 KB |
testcase_01 | AC | 60 ms
38,132 KB |
testcase_02 | AC | 84 ms
38,500 KB |
testcase_03 | AC | 73 ms
38,392 KB |
testcase_04 | AC | 72 ms
38,208 KB |
testcase_05 | AC | 85 ms
38,580 KB |
testcase_06 | AC | 82 ms
38,396 KB |
testcase_07 | AC | 1,353 ms
113,476 KB |
testcase_08 | AC | 1,377 ms
113,060 KB |
testcase_09 | AC | 1,398 ms
113,724 KB |
testcase_10 | AC | 1,318 ms
113,536 KB |
testcase_11 | AC | 1,333 ms
113,808 KB |
testcase_12 | AC | 1,377 ms
113,544 KB |
testcase_13 | AC | 1,340 ms
113,380 KB |
testcase_14 | AC | 1,376 ms
113,504 KB |
testcase_15 | AC | 1,445 ms
113,888 KB |
testcase_16 | AC | 1,326 ms
113,680 KB |
testcase_17 | AC | 1,353 ms
113,504 KB |
testcase_18 | AC | 1,353 ms
113,380 KB |
testcase_19 | AC | 1,305 ms
113,316 KB |
testcase_20 | AC | 1,353 ms
113,540 KB |
testcase_21 | AC | 1,378 ms
113,348 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { static List<Map<Integer, Integer>> graph = new ArrayList<>(); static int[] depths; static int[][] parents; static long[] values; 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); } depths = new int[n]; parents = new int[20][n]; values = new long[n]; setDepth(0, 0, 0, 0); for (int i = 1; i < 20; i++) { for (int j = 0; j < n; j++) { parents[i][j] = parents[i - 1][parents[i - 1][j]]; } } int q = sc.nextInt(); System.out.println(IntStream.range(0, q) .mapToObj(i -> String.valueOf(getSize(new int[]{sc.nextInt(), sc.nextInt(), sc.nextInt()}))) .collect(Collectors.joining("\n"))); } static void setDepth(int idx, int p, int d, long v) { depths[idx] = d; parents[0][idx] = p; values[idx] = v; for (int x : graph.get(idx).keySet()) { if (x != p) { setDepth(x, idx, d + 1, v + graph.get(idx).get(x)); } } } static long getSize(int[] points) { long ans = 0; for (int i = 0; i < points.length; i++) { ans += getSize(points[i], points[(i + 1) % points.length]); } return ans / 2; } static long getSize(int a, int b) { return values[a] + values[b] - values[getLCA(a, b)] * 2; } static int getLCA(int a, int b) { if (depths[a] < depths[b]) { return getLCA(b, a); } for (int i = 19; i >= 0; i--) { if (depths[a] - depths[b] >= (1 << i)) { a = parents[i][a]; } } if (a == b) { return a; } for (int i = 19; i >= 0; i--) { if (parents[i][a] != parents[i][b]) { a = parents[i][a]; b = parents[i][b]; } } return parents[0][a]; } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }