結果

問題 No.277 根掘り葉掘り
ユーザー koyumeishikoyumeishi
提出日時 2015-09-03 00:53:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 211 ms / 3,000 ms
コード長 719 bytes
コンパイル時間 615 ms
コンパイル使用メモリ 68,484 KB
実行使用メモリ 9,748 KB
最終ジャッジ日時 2023-09-26 02:30:18
合計ジャッジ時間 5,142 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 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 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 207 ms
8,940 KB
testcase_10 AC 190 ms
9,748 KB
testcase_11 AC 211 ms
9,244 KB
testcase_12 AC 207 ms
9,136 KB
testcase_13 AC 209 ms
9,404 KB
testcase_14 AC 206 ms
9,248 KB
testcase_15 AC 208 ms
9,172 KB
testcase_16 AC 205 ms
9,244 KB
testcase_17 AC 206 ms
9,228 KB
testcase_18 AC 205 ms
9,132 KB
testcase_19 AC 209 ms
9,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <cassert>

using namespace std;
int main(){
	int n;
	cin >> n;
	assert(2<=n && n<=100000);

	vector<vector<int>> G(n);
	for(int i=0; i<n-1; i++){
		int x,y;
		cin >> x >> y;
		assert(1<=x);
		assert(x< y);
		assert(y<=n);

		x--; y--;
		G[x].push_back(y);
		G[y].push_back(x);
	}

	vector<int> dist(n, 1e8);

	queue<int> q;
	q.push(0);
	dist[0] = 0;
	for(int i=1; i<n; i++){
		if(G[i].size()==1){
			q.push(i);
			dist[i] = 0;
		}
	}

	while(q.size()){
		int pos = q.front();
		q.pop();
		for(int to : G[pos]){
			if(dist[to] > dist[pos]+1){
				dist[to] = dist[pos]+1;
				q.push(to);
			}
		}
	}

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