結果

問題 No.277 根掘り葉掘り
ユーザー finefine
提出日時 2017-03-24 18:45:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 201 ms / 3,000 ms
コード長 849 bytes
コンパイル時間 1,852 ms
コンパイル使用メモリ 172,320 KB
実行使用メモリ 17,060 KB
最終ジャッジ日時 2023-09-20 06:06:48
合計ジャッジ時間 5,262 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 201 ms
17,060 KB
testcase_10 AC 167 ms
9,712 KB
testcase_11 AC 179 ms
9,320 KB
testcase_12 AC 195 ms
13,260 KB
testcase_13 AC 193 ms
9,320 KB
testcase_14 AC 175 ms
9,576 KB
testcase_15 AC 181 ms
10,120 KB
testcase_16 AC 180 ms
9,748 KB
testcase_17 AC 181 ms
9,628 KB
testcase_18 AC 183 ms
9,656 KB
testcase_19 AC 181 ms
9,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const int INF = 1e7;

vector< vector<int> > G;
vector<int> R, L;

void dfs(int cur, int par) {
	for (int nex : G[cur]) {
		if (nex == par) continue;
		R[nex] = R[cur] + 1;
		dfs(nex, cur);
		L[cur] = min(L[cur], L[nex]);
	}
	if (L[cur] == INF) L[cur] = 0;
	else L[cur]++;
}

void dfs2(int cur, int par) {
	if (par != -1) L[cur] = min(L[cur], L[par] + 1);
	for (int nex : G[cur]) {
		if (nex == par) continue;
		dfs2(nex, cur);
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	G.resize(n);
	for (int i = 1; i < n; i++) {
		int x, y;
		cin >> x >> y;
		x--;
		y--;
		G[x].push_back(y);
		G[y].push_back(x);
	}

	R.resize(n, INF);
	L.resize(n, INF);
	R[0] = 0;
	dfs(0, -1);
	dfs2(0, -1);
	
	for (int i = 0; i < n; i++) {
		cout << min(R[i], L[i]) << endl;
	}
	return 0;
}
0