結果

問題 No.277 根掘り葉掘り
ユーザー fiordfiord
提出日時 2015-09-08 11:17:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 214 ms / 3,000 ms
コード長 587 bytes
コンパイル時間 1,340 ms
コンパイル使用メモリ 150,752 KB
実行使用メモリ 9,668 KB
最終ジャッジ日時 2023-09-26 10:06:55
合計ジャッジ時間 5,438 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 212 ms
8,816 KB
testcase_10 AC 191 ms
9,668 KB
testcase_11 AC 212 ms
9,112 KB
testcase_12 AC 211 ms
9,112 KB
testcase_13 AC 214 ms
9,400 KB
testcase_14 AC 212 ms
9,444 KB
testcase_15 AC 211 ms
9,164 KB
testcase_16 AC 208 ms
9,324 KB
testcase_17 AC 211 ms
9,248 KB
testcase_18 AC 208 ms
9,132 KB
testcase_19 AC 206 ms
9,332 KB
権限があれば一括ダウンロードができます

ソースコード

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> ans(n,(1<<30));
	queue<int> q;	ans[0]=0,q.push(0);	
	for(int i=1;i<n;i++){
		if(ls[i].size()==1){
			ans[i]=0,q.push(i);
		}
	}
	while(!q.empty()){
		int now=q.front();	q.pop();
		for(int i=0;i<ls[now].size();i++){
			int next=ls[now][i];
			if(ans[next]>ans[now]+1){
				ans[next]=ans[now]+1;
				q.push(next);
			}
		}
	}
	for(int i=0;i<n;i++)	cout<<ans[i]<<endl;
	return 0;
}
0