結果

問題 No.277 根掘り葉掘り
ユーザー fiordfiord
提出日時 2015-09-07 20:59:48
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 909 bytes
コンパイル時間 1,363 ms
コンパイル使用メモリ 151,520 KB
実行使用メモリ 14,144 KB
最終ジャッジ日時 2023-09-26 10:02:14
合計ジャッジ時間 7,146 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
14,144 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 218 ms
9,320 KB
testcase_10 AC 195 ms
11,124 KB
testcase_11 AC 220 ms
9,812 KB
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;	cin>>n;
	vector<int> ls[n];
	int x,y;
	for(int i=1;i<n;i++){
		cin>>x>>y;	x--;	y--;
		ls[x].push_back(y);	ls[y].push_back(x);
	}
	vector<int> r(n),l(n);
	for(int i=0;i<n;i++)	r[i]=l[i]=(1<<30);
	queue<int> s;	s.push(0);
	vector<int> leaf;
	r[0]=0;
	while(!s.empty()){
		int now=s.front();	s.pop();
		bool lf=true;
		for(int i=0;i<(int)ls[now].size();i++){
			int next=ls[now][i];
			if(r[next]>r[now]+1){
				lf=false;
				r[next]=r[now]+1;
				s.push(next);
			}
		}
		if(lf)	leaf.push_back(now);
	}
	for(int i=0;i<(int)leaf.size();i++){
		s.push(leaf[i]);
		l[leaf[i]]=0;
		while(!s.empty()){
			int now=s.front();	s.pop();
			for(int j=0;j<(int)ls[now].size();j++){
				int next=ls[now][j];
				if(l[next]>l[now]+1){
					l[next]=l[now]+1;
					s.push(next);
				}
			}
		}
	}
	for(int i=0;i<n;i++)	cout<<min(l[i],r[i])<<endl;
	return 0;
}
0