結果

問題 No.277 根掘り葉掘り
ユーザー snrnsidysnrnsidy
提出日時 2021-07-18 23:36:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 856 bytes
コンパイル時間 2,148 ms
コンパイル使用メモリ 201,240 KB
実行使用メモリ 15,852 KB
最終ジャッジ日時 2023-09-23 03:02:03
合計ジャッジ時間 5,167 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,040 KB
testcase_01 AC 4 ms
6,072 KB
testcase_02 WA -
testcase_03 AC 4 ms
6,052 KB
testcase_04 AC 4 ms
6,172 KB
testcase_05 AC 4 ms
6,116 KB
testcase_06 AC 3 ms
6,076 KB
testcase_07 AC 3 ms
6,064 KB
testcase_08 AC 4 ms
6,084 KB
testcase_09 AC 64 ms
15,852 KB
testcase_10 AC 38 ms
9,628 KB
testcase_11 AC 54 ms
9,636 KB
testcase_12 AC 62 ms
12,900 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 R[100005];
int L[100005];
vector <int> adj[100005];
int n, x, y;

void dfs(int now,int prev)
{
	for (auto next : adj[now])
	{
		if (prev == next) continue;
		R[next] = R[now] + 1;
		dfs(next, now);
	}
}

int solve(int now, int prev)
{
	if (L[now] != -1)
	{
		return L[now];
	}
	int res = 1e9;
	for (auto next : adj[now])
	{
		if (prev == next)
		{
			continue;
		}
		res = min(res, solve(next, now) + 1);
	}
	if (res >= 1e9)
	{
		res = 0;
	}
	L[now] = res;
	return L[now];
}

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	cin >> n;

	for (int i = 0; i < n - 1; i++)
	{
		cin >> x >> y;
		adj[x].push_back(y);
		adj[y].push_back(x);
	}

	memset(L, -1, sizeof(L));
	R[1] = 0;
	dfs(1, 0);
	solve(1, 0);

	for (int i = 1; i <= n; i++)
	{
		cout << min(L[i], R[i]) << '\n';
	}


	return 0;
}
0