結果

問題 No.277 根掘り葉掘り
ユーザー pekempeypekempey
提出日時 2015-09-04 22:42:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 238 ms / 3,000 ms
コード長 1,113 bytes
コンパイル時間 1,647 ms
コンパイル使用メモリ 146,376 KB
実行使用メモリ 15,860 KB
最終ジャッジ日時 2023-09-26 05:58:49
合計ジャッジ時間 5,837 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,856 KB
testcase_01 AC 4 ms
5,740 KB
testcase_02 AC 3 ms
5,732 KB
testcase_03 AC 3 ms
5,724 KB
testcase_04 AC 4 ms
5,784 KB
testcase_05 AC 3 ms
5,896 KB
testcase_06 AC 4 ms
5,804 KB
testcase_07 AC 4 ms
5,784 KB
testcase_08 AC 4 ms
5,736 KB
testcase_09 AC 238 ms
15,860 KB
testcase_10 AC 189 ms
9,948 KB
testcase_11 AC 215 ms
9,636 KB
testcase_12 AC 223 ms
12,748 KB
testcase_13 AC 227 ms
9,872 KB
testcase_14 AC 217 ms
9,980 KB
testcase_15 AC 222 ms
10,368 KB
testcase_16 AC 216 ms
10,040 KB
testcase_17 AC 217 ms
10,028 KB
testcase_18 AC 221 ms
9,912 KB
testcase_19 AC 219 ms
9,892 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