結果

問題 No.277 根掘り葉掘り
ユーザー 37kt_37kt_
提出日時 2015-10-17 22:36:21
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 542 bytes
コンパイル時間 1,794 ms
コンパイル使用メモリ 146,636 KB
実行使用メモリ 15,860 KB
最終ジャッジ日時 2023-09-29 16:57:11
合計ジャッジ時間 4,423 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,764 KB
testcase_01 AC 3 ms
5,816 KB
testcase_02 WA -
testcase_03 AC 3 ms
5,768 KB
testcase_04 AC 3 ms
5,992 KB
testcase_05 AC 3 ms
5,896 KB
testcase_06 AC 3 ms
5,708 KB
testcase_07 AC 3 ms
5,712 KB
testcase_08 AC 3 ms
5,756 KB
testcase_09 AC 196 ms
15,860 KB
testcase_10 AC 173 ms
9,600 KB
testcase_11 AC 182 ms
9,548 KB
testcase_12 AC 193 ms
12,732 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>
using namespace std;

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

void dfs(int v, int p)
{
	bool f = true;
	for (int t : g[v]){
		if (t != p){
			r[t] = r[v] + 1;
			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