結果

問題 No.277 根掘り葉掘り
ユーザー 夜
提出日時 2021-02-15 21:14:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,229 bytes
コンパイル時間 894 ms
コンパイル使用メモリ 100,728 KB
実行使用メモリ 10,744 KB
最終ジャッジ日時 2023-09-30 19:11:43
合計ジャッジ時間 4,678 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
bool bo[100010] = { false };
int r[100010], l[100010];
vector<vector<int>> vec(100010);
int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		int x, y;
		cin >> x >> y;
		vec[x].emplace_back(y);
		vec[y].emplace_back(x);
		bo[x] = true;
	}
	queue<int> que, que1;
	que.push(1);
	for (int i = 1; i <= n; i++) {
		r[i] = 1000000007, l[i] = 1000000007;
		if (!bo[i]) {
			que1.push(i);
			l[i] = 0;
		}
	}
	r[1] = 0;
	while (!que.empty()) {
		int now = que.front();
		que.pop();
		for (int i = 0; i < vec[now].size(); i++) {
			if (r[vec[now][i]] == 1000000007) {
				r[vec[now][i]] = r[now] + 1;
				que.push(vec[now][i]);
			}
		}
	}
	while (!que1.empty()) {
		int now = que1.front();
		que1.pop();
		for (int i = 0; i < vec[now].size(); i++) {
			if (l[vec[now][i]] > l[now] + 1) {
				l[vec[now][i]] = l[now] + 1;
				que1.push(vec[now][i]);
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		cout << min(r[i], l[i]) << endl;
	}
}
0