結果

問題 No.277 根掘り葉掘り
ユーザー t8m8⛄️t8m8⛄️
提出日時 2015-09-04 22:56:09
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,601 bytes
コンパイル時間 3,979 ms
コンパイル使用メモリ 76,016 KB
実行使用メモリ 80,572 KB
最終ジャッジ日時 2023-09-26 06:11:25
合計ジャッジ時間 15,797 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,840 KB
testcase_01 AC 125 ms
55,660 KB
testcase_02 WA -
testcase_03 AC 146 ms
55,544 KB
testcase_04 AC 142 ms
56,008 KB
testcase_05 AC 143 ms
55,872 KB
testcase_06 AC 143 ms
56,004 KB
testcase_07 AC 144 ms
55,772 KB
testcase_08 AC 124 ms
56,204 KB
testcase_09 AC 897 ms
80,572 KB
testcase_10 AC 848 ms
68,376 KB
testcase_11 AC 857 ms
68,852 KB
testcase_12 AC 863 ms
76,264 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
import java.awt.geom.*;
import java.math.*;

public class No0277 {

	static final Scanner in = new Scanner(System.in);
	static final PrintWriter out = new PrintWriter(System.out,false);

	static void solve() {
		int n = in.nextInt();
		res = new int[n];
		Arrays.fill(res,Integer.MAX_VALUE);
		int[] s = new int[n-1];
		int[] t = new int[n-1];

		for (int i=0; i<n-1; i++) {
			s[i] = in.nextInt()-1;
			t[i] = in.nextInt()-1;
		}

		g = toAdjacencyList(n,s,t);

		rec(0,0,-1);

		for (int i=0; i<n; i++) {
			out.println(res[i]);
		}
	}

	static int[] res;
	static int[][] g;

	static int rec(int cur, int depth, int prev) {
		res[cur] = depth;
		int t = Integer.MAX_VALUE;
		boolean f = false;
		for (int to : g[cur]) {
			if (to == prev) continue;
			f = true;
			t = Math.min(t, rec(to, depth+1, cur) + 1);
		}
		if (!f) return res[cur] = 0;
		res[cur] = Math.min(res[cur],t);
		return t;
	}

	static int[][] toAdjacencyList(int n,int[] from, int[] to) {
		int[] cnt = new int[n];
		for (int i : from) cnt[i]++;
		for (int i : to) cnt[i]++;

		int[][] g = new int[n][];
		for (int i=0; i<n; i++) g[i] = new int[cnt[i]];
		for (int i=0; i<from.length; i++) {
			int f = from[i];
			int t = to[i];
			g[f][--cnt[f]] = t;
			g[t][--cnt[t]] = f;
		}

		return g;
	}

	public static void main(String[] args) {
		long start = System.currentTimeMillis();

		solve();
		out.flush();

		long end = System.currentTimeMillis();
		//trace(end-start + "ms");
		in.close();
		out.close();
	}

	static void trace(Object... o) { System.out.println(Arrays.deepToString(o));}
}
0