結果
問題 | No.277 根掘り葉掘り |
ユーザー |
|
提出日時 | 2015-09-05 09:09:22 |
言語 | Java17 (openjdk 17.0.1) |
結果 |
AC
|
実行時間 | 738 ms / 3,000 ms |
コード長 | 2,602 bytes |
コンパイル時間 | 3,449 ms |
使用メモリ | 63,928 KB |
最終ジャッジ日時 | 2023-02-17 05:59:22 |
合計ジャッジ時間 | 12,008 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge14 |
テストケース
テストケース表示入力 | 結果 | 実行時間 使用メモリ |
---|---|---|
testcase_00 | AC | 53 ms
35,752 KB |
testcase_01 | AC | 53 ms
35,836 KB |
testcase_02 | AC | 53 ms
35,844 KB |
testcase_03 | AC | 55 ms
35,868 KB |
testcase_04 | AC | 57 ms
37,872 KB |
testcase_05 | AC | 55 ms
35,912 KB |
testcase_06 | AC | 55 ms
37,928 KB |
testcase_07 | AC | 55 ms
36,124 KB |
testcase_08 | AC | 52 ms
35,840 KB |
testcase_09 | AC | 581 ms
57,640 KB |
testcase_10 | AC | 513 ms
63,928 KB |
testcase_11 | AC | 570 ms
60,828 KB |
testcase_12 | AC | 550 ms
61,064 KB |
testcase_13 | AC | 595 ms
60,516 KB |
testcase_14 | AC | 530 ms
58,248 KB |
testcase_15 | AC | 603 ms
59,676 KB |
testcase_16 | AC | 587 ms
62,668 KB |
testcase_17 | AC | 738 ms
62,256 KB |
testcase_18 | AC | 573 ms
59,456 KB |
testcase_19 | AC | 604 ms
61,384 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); } } }