結果

問題 No.277 根掘り葉掘り
ユーザー 37kt_
提出日時 2015-10-17 22:46:18
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 235 ms / 3,000 ms
コード長 621 bytes
コンパイル時間 1,851 ms
コンパイル使用メモリ 164,308 KB
実行使用メモリ 9,848 KB
最終ジャッジ日時 2024-07-21 16:36:49
合計ジャッジ時間 5,943 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

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