結果

問題 No.277 根掘り葉掘り
ユーザー 37kt_37kt_
提出日時 2015-10-17 22:46:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 220 ms / 3,000 ms
コード長 621 bytes
コンパイル時間 1,371 ms
コンパイル使用メモリ 150,308 KB
実行使用メモリ 9,588 KB
最終ジャッジ日時 2023-09-28 21:49:54
合計ジャッジ時間 5,685 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,752 KB
testcase_01 AC 3 ms
5,680 KB
testcase_02 AC 4 ms
5,724 KB
testcase_03 AC 4 ms
5,676 KB
testcase_04 AC 3 ms
5,692 KB
testcase_05 AC 4 ms
5,692 KB
testcase_06 AC 4 ms
5,684 KB
testcase_07 AC 4 ms
5,812 KB
testcase_08 AC 3 ms
5,680 KB
testcase_09 AC 205 ms
9,256 KB
testcase_10 AC 188 ms
9,588 KB
testcase_11 AC 215 ms
9,388 KB
testcase_12 AC 215 ms
9,396 KB
testcase_13 AC 220 ms
9,568 KB
testcase_14 AC 211 ms
9,448 KB
testcase_15 AC 211 ms
9,536 KB
testcase_16 AC 209 ms
9,420 KB
testcase_17 AC 210 ms
9,504 KB
testcase_18 AC 205 ms
9,496 KB
testcase_19 AC 212 ms
9,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int n;
vector<int> g[100000];
int dist[100000];

int main()
{
	cin >> n;
	for (int i = 0; i < n - 1; i++){
		int x, y;
		cin >> x >> y;
		x--; y--;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	
	fill_n(dist, n, 1 << 28);
	queue<int> q;
	dist[0] = 0;
	q.push(0);
	for (int i = 0; i < n; i++){
		if (g[i].size() == 1){
			dist[i] = 0;
			q.push(i);
		}
	}
	while (q.size()){
		int p = q.front(); q.pop();
		for (int t : g[p]){
			if (dist[t] > dist[p] + 1){
				dist[t] = dist[p] + 1;
				q.push(t);
			}
		}
	}
	
	for (int i = 0; i < n; i++){
		cout << dist[i] << endl;
	}
}
0