結果

問題 No.277 根掘り葉掘り
ユーザー masamasa
提出日時 2015-09-05 00:41:55
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 877 bytes
コンパイル時間 1,912 ms
コンパイル使用メモリ 73,744 KB
実行使用メモリ 758,560 KB
最終ジャッジ日時 2023-09-26 07:42:54
合計ジャッジ時間 6,358 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 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,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 MLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

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 (z < 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