結果
問題 | No.277 根掘り葉掘り |
ユーザー | tenten |
提出日時 | 2020-10-05 09:56:46 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,360 bytes |
コンパイル時間 | 2,348 ms |
コンパイル使用メモリ | 76,788 KB |
実行使用メモリ | 97,232 KB |
最終ジャッジ日時 | 2023-09-27 01:48:13 |
合計ジャッジ時間 | 16,593 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 121 ms
56,136 KB |
testcase_01 | AC | 121 ms
56,052 KB |
testcase_02 | WA | - |
testcase_03 | AC | 134 ms
56,124 KB |
testcase_04 | AC | 134 ms
56,776 KB |
testcase_05 | AC | 145 ms
56,064 KB |
testcase_06 | AC | 139 ms
55,888 KB |
testcase_07 | AC | 140 ms
55,980 KB |
testcase_08 | AC | 121 ms
55,464 KB |
testcase_09 | AC | 1,052 ms
97,232 KB |
testcase_10 | AC | 879 ms
76,784 KB |
testcase_11 | AC | 981 ms
79,172 KB |
testcase_12 | AC | 1,005 ms
87,308 KB |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
ソースコード
import java.util.*; public class Main { static ArrayList<ArrayList<Integer>> graph = new ArrayList<>(); static int[] roots; static int[] leaves; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); roots = new int[n]; leaves = new int[n]; 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); } getDist(0, 0, 0); StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(Math.min(roots[i], leaves[i])).append("\n"); } System.out.print(sb); } static int getDist(int idx, int parent, int depth) { roots[idx] = depth; if (idx != 0 && graph.get(idx).size() == 1) { leaves[idx] = 0; } else { int min = Integer.MAX_VALUE; for (int x : graph.get(idx)) { if (x == parent) { continue; } min = Math.min(min, getDist(x, idx, depth + 1)); } leaves[idx] = min; } return leaves[idx] + 1; } }