結果
問題 | No.277 根掘り葉掘り |
ユーザー | Grenache |
提出日時 | 2015-09-05 09:09:22 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 705 ms / 3,000 ms |
コード長 | 2,602 bytes |
コンパイル時間 | 6,311 ms |
コンパイル使用メモリ | 75,124 KB |
実行使用メモリ | 77,316 KB |
最終ジャッジ日時 | 2023-09-26 08:44:47 |
合計ジャッジ時間 | 13,498 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
49,544 KB |
testcase_01 | AC | 44 ms
49,440 KB |
testcase_02 | AC | 43 ms
47,444 KB |
testcase_03 | AC | 46 ms
49,488 KB |
testcase_04 | AC | 47 ms
49,372 KB |
testcase_05 | AC | 46 ms
49,556 KB |
testcase_06 | AC | 47 ms
49,468 KB |
testcase_07 | AC | 48 ms
49,460 KB |
testcase_08 | AC | 43 ms
49,420 KB |
testcase_09 | AC | 666 ms
74,756 KB |
testcase_10 | AC | 587 ms
74,628 KB |
testcase_11 | AC | 661 ms
74,792 KB |
testcase_12 | AC | 680 ms
76,452 KB |
testcase_13 | AC | 705 ms
74,216 KB |
testcase_14 | AC | 684 ms
76,936 KB |
testcase_15 | AC | 696 ms
73,476 KB |
testcase_16 | AC | 589 ms
76,104 KB |
testcase_17 | AC | 687 ms
77,316 KB |
testcase_18 | AC | 673 ms
76,504 KB |
testcase_19 | AC | 685 ms
76,524 KB |
ソースコード
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Queue; public class Main_yukicoder277 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Printer pr = new Printer(System.out); int n = sc.nextInt(); List<ArrayList<Integer>> edges = new ArrayList<ArrayList<Integer>>(); for (int i = 0; i < n; i++) { edges.add(new ArrayList<Integer>()); } for (int i = 0; i < n - 1; i++) { int x = sc.nextInt() - 1; int y = sc.nextInt() - 1; edges.get(x).add(y); edges.get(y).add(x); } Queue<Integer> q = new ArrayDeque<Integer>(); int[] d = new int[n]; final int INF = Integer.MAX_VALUE; Arrays.fill(d, INF); q.add(0); d[0] = 0; for (int i = 0; i < n; i++) { if (edges.get(i).size() == 1) { q.add(i); d[i] = 0; } } while (!q.isEmpty()) { int u = q.remove(); for (int e : edges.get(u)) { if (d[e] > d[u] + 1) { q.add(e); d[e] = d[u] + 1; } } } for (int e : d) { pr.println(e); } pr.close(); sc.close(); } @SuppressWarnings("unused") private static class Scanner { BufferedReader br; Iterator<String> it; Scanner (InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } String next() throws RuntimeException { try { if (it == null || !it.hasNext()) { it = Arrays.asList(br.readLine().split(" ")).iterator(); } return it.next(); } catch (IOException e) { throw new IllegalStateException(); } } int nextInt() throws RuntimeException { return Integer.parseInt(next()); } long nextLong() throws RuntimeException { return Long.parseLong(next()); } float nextFloat() throws RuntimeException { return Float.parseFloat(next()); } double nextDouble() throws RuntimeException { return Double.parseDouble(next()); } void close() { try { br.close(); } catch (IOException e) { // throw new IllegalStateException(); } } } private static class Printer extends PrintWriter { Printer(PrintStream out) { super(out); } } }