結果

問題 No.277 根掘り葉掘り
ユーザー kyuridenamidakyuridenamida
提出日時 2015-09-04 22:35:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 239 ms / 3,000 ms
コード長 630 bytes
コンパイル時間 4,061 ms
コンパイル使用メモリ 149,704 KB
実行使用メモリ 10,168 KB
最終ジャッジ日時 2023-09-26 05:35:42
合計ジャッジ時間 6,218 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,136 KB
testcase_01 AC 4 ms
6,052 KB
testcase_02 AC 3 ms
6,080 KB
testcase_03 AC 4 ms
6,236 KB
testcase_04 AC 5 ms
6,084 KB
testcase_05 AC 4 ms
6,072 KB
testcase_06 AC 4 ms
6,220 KB
testcase_07 AC 4 ms
6,148 KB
testcase_08 AC 4 ms
6,140 KB
testcase_09 AC 224 ms
9,620 KB
testcase_10 AC 214 ms
10,168 KB
testcase_11 AC 239 ms
9,996 KB
testcase_12 AC 237 ms
9,780 KB
testcase_13 AC 231 ms
9,976 KB
testcase_14 AC 224 ms
10,068 KB
testcase_15 AC 224 ms
9,884 KB
testcase_16 AC 219 ms
9,796 KB
testcase_17 AC 224 ms
9,852 KB
testcase_18 AC 223 ms
9,856 KB
testcase_19 AC 218 ms
9,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int cost[100010];
vector<int> g[100010];

int deg[100010];
int main(){
	memset(cost,-1,sizeof(cost));
	int n;
	cin >> n;
	queue<int> Q;
	for(int i = 1 ; i < n ; i++){
		int a,b;
		cin >> a >> b;
		--a,--b;
		g[a].push_back(b);
		g[b].push_back(a);
		deg[a]++;
		deg[b]++;
	}
	for(int i = 0 ; i < n ; i++){
		if( i == 0 || deg[i] == 1 ){
			Q.push(i);
			cost[i] = 0;
		}
	}
	while(Q.size()){
		int q = Q.front(); Q.pop();
		for( auto e : g[q] ){
			if( cost[e] == -1 ){
				cost[e] = cost[q] + 1;
				Q.push(e);
			}
		}
	}
	for(int i = 0 ; i < n ; i++)
		cout << cost[i] << endl;
}
0