結果

問題 No.277 根掘り葉掘り
ユーザー fiordfiord
提出日時 2015-09-08 11:15:38
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 553 bytes
コンパイル時間 1,677 ms
コンパイル使用メモリ 150,148 KB
実行使用メモリ 9,680 KB
最終ジャッジ日時 2023-09-26 10:06:18
合計ジャッジ時間 5,401 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 209 ms
8,836 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 209 ms
9,392 KB
testcase_14 AC 205 ms
9,460 KB
testcase_15 AC 208 ms
9,128 KB
testcase_16 AC 212 ms
9,368 KB
testcase_17 AC 209 ms
9,220 KB
testcase_18 AC 210 ms
9,100 KB
testcase_19 AC 208 ms
9,312 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;	
	for(int i=0;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