結果

問題 No.277 根掘り葉掘り
ユーザー pekempeypekempey
提出日時 2015-09-04 22:42:45
言語 C++11
(gcc 8.5.0)
結果
AC  
実行時間 223 ms / 3,000 ms
コード長 1,113 bytes
コンパイル時間 1,754 ms
使用メモリ 16,056 KB
最終ジャッジ日時 2023-02-17 02:52:20
合計ジャッジ時間 5,421 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 2 ms
6,272 KB
testcase_01 AC 2 ms
6,168 KB
testcase_02 AC 2 ms
6,196 KB
testcase_03 AC 2 ms
6,256 KB
testcase_04 AC 2 ms
6,280 KB
testcase_05 AC 3 ms
6,192 KB
testcase_06 AC 3 ms
6,208 KB
testcase_07 AC 3 ms
6,172 KB
testcase_08 AC 2 ms
6,208 KB
testcase_09 AC 223 ms
16,056 KB
testcase_10 AC 186 ms
9,676 KB
testcase_11 AC 203 ms
9,712 KB
testcase_12 AC 223 ms
12,924 KB
testcase_13 AC 222 ms
9,900 KB
testcase_14 AC 211 ms
10,192 KB
testcase_15 AC 211 ms
10,388 KB
testcase_16 AC 207 ms
10,144 KB
testcase_17 AC 205 ms
9,996 KB
testcase_18 AC 204 ms
9,972 KB
testcase_19 AC 197 ms
9,936 KB
権限があれば一括ダウンロードができます

ソースコード

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;
}

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

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);
	dfs3(0, -1);
	rep (i, N) {
		cout << min(R[i], L[i])	<< endl;
	}

	return 0;
}
0