結果

問題 No.277 根掘り葉掘り
ユーザー masamasa
提出日時 2015-09-05 00:44:58
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 221 ms / 3,000 ms
コード長 887 bytes
コンパイル時間 829 ms
コンパイル使用メモリ 72,856 KB
実行使用メモリ 10,244 KB
最終ジャッジ日時 2023-09-26 07:43:07
合計ジャッジ時間 4,520 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 214 ms
8,784 KB
testcase_10 AC 192 ms
10,244 KB
testcase_11 AC 216 ms
9,304 KB
testcase_12 AC 213 ms
9,416 KB
testcase_13 AC 221 ms
9,312 KB
testcase_14 AC 212 ms
9,572 KB
testcase_15 AC 216 ms
9,440 KB
testcase_16 AC 213 ms
9,376 KB
testcase_17 AC 210 ms
9,432 KB
testcase_18 AC 214 ms
9,308 KB
testcase_19 AC 214 ms
9,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>

using namespace std;

const int INF = 1e8;

int main() {
	int n, x, y;

	cin >> n;
	vector< vector<int> > next(n+1, vector<int>());

	for (int i = 1; i < n; i++) {
		cin >> x >> y;

		next[x].push_back(y);
		next[y].push_back(x);
	}

	queue<pair<int, int> > que;
	pair<int, int> p;

	vector<int> dist(n+1, INF);
	for (int i = 1; i <= n; i++) {
		if (i == 1 || next[i].size() == 1) {
			dist[i] = 0;
			que.push(make_pair(i, 0));
		}
	}

	int z;

	while (!que.empty()) {
		p = que.front();
		que.pop();

		for (int i = 0; i < next[p.first].size(); i++) {
			z = next[p.first][i];
			if (p.second + 1 < dist[z]) {
				dist[z] = p.second + 1;
				que.push(make_pair(z, p.second + 1));
			}
		}
	}

	for (int i = 1; i <= n; i++) {
		cout << dist[i] << endl;
	}



	return 0;
}
0