結果
問題 | No.277 根掘り葉掘り |
ユーザー | tenten |
提出日時 | 2020-12-23 13:53:30 |
言語 | Java19 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,497 ms / 3,000 ms |
コード長 | 1,595 bytes |
コンパイル時間 | 2,552 ms |
コンパイル使用メモリ | 82,992 KB |
実行使用メモリ | 92,528 KB |
最終ジャッジ日時 | 2023-10-21 15:09:23 |
合計ジャッジ時間 | 20,403 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge9 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 134 ms
57,752 KB |
testcase_01 | AC | 136 ms
57,508 KB |
testcase_02 | AC | 135 ms
57,508 KB |
testcase_03 | AC | 151 ms
57,544 KB |
testcase_04 | AC | 155 ms
57,760 KB |
testcase_05 | AC | 158 ms
57,780 KB |
testcase_06 | AC | 160 ms
57,636 KB |
testcase_07 | AC | 154 ms
57,844 KB |
testcase_08 | AC | 135 ms
57,572 KB |
testcase_09 | AC | 1,131 ms
88,788 KB |
testcase_10 | AC | 1,213 ms
89,248 KB |
testcase_11 | AC | 1,399 ms
89,788 KB |
testcase_12 | AC | 1,497 ms
92,528 KB |
testcase_13 | AC | 1,370 ms
89,112 KB |
testcase_14 | AC | 1,277 ms
88,264 KB |
testcase_15 | AC | 1,254 ms
88,736 KB |
testcase_16 | AC | 1,252 ms
88,268 KB |
testcase_17 | AC | 1,216 ms
88,636 KB |
testcase_18 | AC | 1,269 ms
89,180 KB |
testcase_19 | AC | 1,257 ms
87,488 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[] depth = new int[n]; Arrays.fill(depth, Integer.MAX_VALUE); PriorityQueue<Path> queue = new PriorityQueue<>(); queue.add(new Path(0, 0)); while (queue.size() > 0) { Path p = queue.poll(); if (depth[p.idx] <= p.value) { continue; } depth[p.idx] = p.value; for (int x : graph.get(p.idx)) { queue.add(new Path(x, p.value + 1)); } if (graph.get(p.idx).size() == 1) { queue.add(new Path(p.idx, 0)); } } StringBuilder sb = new StringBuilder(); for (int x : depth) { 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; } } }