結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,680 KB
testcase_01 AC 3 ms
5,712 KB
testcase_02 WA -
testcase_03 AC 3 ms
5,724 KB
testcase_04 AC 3 ms
5,692 KB
testcase_05 AC 4 ms
5,952 KB
testcase_06 AC 3 ms
5,728 KB
testcase_07 AC 3 ms
5,784 KB
testcase_08 AC 3 ms
5,648 KB
testcase_09 AC 229 ms
16,108 KB
testcase_10 AC 187 ms
9,560 KB
testcase_11 AC 206 ms
9,740 KB
testcase_12 AC 212 ms
12,740 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)
{
	for (int t : g[v]){
		if (t != p){
			r[t] = r[v] + 1;
			dfs(t, v);
		}
	}
}

void dfs2(int v, int p)
{
	bool f = true;
	for (int t : g[v]){
		if (t != p){
			dfs2(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);
	dfs2(0, -1);
	
	for (int i = 0; i < n; i++){
		cout << min(r[i], l[i]) << endl;
	}
}
0