結果

問題 No.277 根掘り葉掘り
ユーザー koyumeishikoyumeishi
提出日時 2015-07-14 02:45:39
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 675 bytes
コンパイル時間 571 ms
コンパイル使用メモリ 62,736 KB
実行使用メモリ 18,296 KB
最終ジャッジ日時 2023-09-22 15:30:57
合計ジャッジ時間 3,862 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 WA -
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 216 ms
18,296 KB
testcase_10 AC 189 ms
9,504 KB
testcase_11 AC 205 ms
8,860 KB
testcase_12 AC 216 ms
13,588 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

//木DPっぽい嘘解法
#include <iostream>
#include <vector>
using namespace std;

const int INF = 1e8;

int dfs(vector<vector<int>>& G, vector<int>& dist, int pos, int last, int d){

	int x = INF;
	for(int i=0; i<G[pos].size(); i++){
		if(G[pos][i] == last) continue;
		x = min(x, dfs(G, dist, G[pos][i], pos, d+1) + 1);
	}
	if(x == INF) x = 0;
	dist[pos] = min(x, d);

	return x;
}

int main(){
	int n;
	cin >> n;
	vector<vector<int>> G(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);
	}

	vector<int> dist(n, INF);
	dfs(G,dist, 0,-1,0);
	for(int i=0; i<n; i++){
		cout << dist[i] << endl;
	}
	return 0;
}
0