結果

問題 No.277 根掘り葉掘り
ユーザー pekempeypekempey
提出日時 2015-09-04 22:37:59
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 956 bytes
コンパイル時間 1,445 ms
コンパイル使用メモリ 146,304 KB
実行使用メモリ 15,876 KB
最終ジャッジ日時 2023-09-26 05:44:32
合計ジャッジ時間 5,026 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,768 KB
testcase_01 AC 3 ms
5,724 KB
testcase_02 WA -
testcase_03 AC 4 ms
5,780 KB
testcase_04 AC 3 ms
5,784 KB
testcase_05 AC 4 ms
5,784 KB
testcase_06 AC 4 ms
5,692 KB
testcase_07 AC 4 ms
5,724 KB
testcase_08 AC 3 ms
5,772 KB
testcase_09 AC 232 ms
15,876 KB
testcase_10 AC 190 ms
9,596 KB
testcase_11 AC 215 ms
9,576 KB
testcase_12 AC 229 ms
12,948 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < (a); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define repr(i, a) for (int i = (a) - 1; i >= 0; i--)
#define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--)
using namespace std;
typedef long long ll;

const int inf = 1e9;
int N;
vector<int> G[101010];
int R[101010];
int L[101010];

void dfs(int curr, int prev) {
	for (int next : G[curr]) if (next != prev) {
		R[next] = R[curr] + 1;
		dfs(next, curr);
	}	
}

int dfs2(int curr, int prev) {
	int res = inf;
	bool leaf = true;
	for (int next : G[curr]) if (next != prev) {
		int d = dfs2(next, curr);
		res = min(res, d + 1);
		leaf = false;
	}
	if (leaf) return L[curr] = 0;
	return L[curr] = res;
}

int main() {
	cin >> N;
	rep (i, N - 1) {
		int x, y;
		cin >> x >> y;
		x--; y--;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	dfs(0, -1);
	dfs2(0, -1);
	rep (i, N) {
		cout << min(R[i], L[i])	<< endl;
	}

	return 0;
}
0