結果
問題 | No.277 根掘り葉掘り |
ユーザー | htensai |
提出日時 | 2020-05-12 11:57:20 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,325 ms / 3,000 ms |
コード長 | 1,327 bytes |
コンパイル時間 | 2,649 ms |
コンパイル使用メモリ | 76,124 KB |
実行使用メモリ | 81,340 KB |
最終ジャッジ日時 | 2023-10-11 07:37:29 |
合計ジャッジ時間 | 18,940 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 126 ms
55,936 KB |
testcase_01 | AC | 125 ms
55,764 KB |
testcase_02 | AC | 127 ms
55,784 KB |
testcase_03 | AC | 147 ms
55,528 KB |
testcase_04 | AC | 144 ms
55,700 KB |
testcase_05 | AC | 147 ms
55,788 KB |
testcase_06 | AC | 149 ms
55,972 KB |
testcase_07 | AC | 146 ms
55,980 KB |
testcase_08 | AC | 128 ms
55,712 KB |
testcase_09 | AC | 1,142 ms
80,876 KB |
testcase_10 | AC | 1,033 ms
81,036 KB |
testcase_11 | AC | 1,325 ms
81,340 KB |
testcase_12 | AC | 1,205 ms
81,316 KB |
testcase_13 | AC | 1,279 ms
80,968 KB |
testcase_14 | AC | 1,244 ms
80,896 KB |
testcase_15 | AC | 1,170 ms
81,096 KB |
testcase_16 | AC | 1,184 ms
81,048 KB |
testcase_17 | AC | 1,227 ms
80,804 KB |
testcase_18 | AC | 1,208 ms
80,780 KB |
testcase_19 | AC | 1,229 ms
81,080 KB |
ソースコード
import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); ArrayList<ArrayList<Integer>> graph = new ArrayList<>(); 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; graph.get(a).add(b); graph.get(b).add(a); } int[] costs = new int[n]; Arrays.fill(costs, Integer.MAX_VALUE); PriorityQueue<Path> queue = new PriorityQueue<>(); queue.add(new Path(0, 0)); while (queue.size() > 0) { Path p = queue.poll(); if (graph.get(p.idx).size() == 1) { p.value = 0; } if (costs[p.idx] <= p.value) { continue; } costs[p.idx] = p.value; for (int x : graph.get(p.idx)) { queue.add(new Path(x, p.value + 1)); } } StringBuilder sb = new StringBuilder(); for (int x : costs) { sb.append(x).append("\n"); } System.out.print(sb); } static class Path implements Comparable<Path> { int idx; int value; public Path(int idx, int value) { this.idx = idx; this.value = value; } public int compareTo(Path another) { return value - another.value; } } }