結果

問題 No.277 根掘り葉掘り
ユーザー 37kt_37kt_
提出日時 2015-10-17 22:40:29
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 599 bytes
コンパイル時間 1,332 ms
コンパイル使用メモリ 146,672 KB
実行使用メモリ 10,056 KB
最終ジャッジ日時 2023-09-29 17:00:03
合計ジャッジ時間 7,143 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
10,056 KB
testcase_01 AC 4 ms
5,668 KB
testcase_02 WA -
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int n;
vector<int> g[100000];
int r[100000], l[100000];

void dfs(int v, int p)
{
	for (int t : g[v]){
		if (t != p){
			r[t] = r[v] + 1;
			dfs(t, v);
		}
	}
	bool f = true;
	for (int t : g[v]){
		if (t != p){
			dfs(t, v);
			l[v] = min(l[v], l[t] + 1);
			f = false;
		}
	}
	if (f) l[v] = 0;
}

int main()
{
	cin >> n;
	for (int i = 0; i < n - 1; i++){
		int x, y;
		cin >> x >> y;
		x--; y--;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	
	fill_n(l, n, 1 << 28);
	dfs(0, -1);
	
	for (int i = 0; i < n; i++){
		cout << min(r[i], l[i]) << endl;
	}
}
0