結果

問題 No.277 根掘り葉掘り
ユーザー たこしたこし
提出日時 2015-11-03 23:48:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 234 ms / 3,000 ms
コード長 2,128 bytes
コンパイル時間 836 ms
コンパイル使用メモリ 97,404 KB
実行使用メモリ 16,292 KB
最終ジャッジ日時 2023-10-11 13:37:52
合計ジャッジ時間 5,595 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,876 KB
testcase_01 AC 4 ms
6,856 KB
testcase_02 AC 4 ms
6,864 KB
testcase_03 AC 4 ms
6,904 KB
testcase_04 AC 4 ms
6,828 KB
testcase_05 AC 4 ms
6,872 KB
testcase_06 AC 4 ms
6,936 KB
testcase_07 AC 4 ms
6,908 KB
testcase_08 AC 4 ms
6,900 KB
testcase_09 AC 234 ms
16,292 KB
testcase_10 AC 198 ms
10,792 KB
testcase_11 AC 217 ms
10,388 KB
testcase_12 AC 222 ms
13,636 KB
testcase_13 AC 224 ms
10,480 KB
testcase_14 AC 217 ms
10,596 KB
testcase_15 AC 219 ms
11,088 KB
testcase_16 AC 224 ms
10,828 KB
testcase_17 AC 220 ms
10,960 KB
testcase_18 AC 217 ms
10,596 KB
testcase_19 AC 218 ms
10,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <queue>
#include <complex>

#define INF 100000000
#define INF_INT_MAX 2147483647
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long

using namespace std;

#define MAX_N 100005

int N;
vector<int> Edge[MAX_N];

class cNode{
public:
	int RootLength;
	int LeafLength;
	bool LeafFlag;

	cNode(){
		RootLength = 0;
		LeafLength = -1;
		LeafFlag = false;
	}

}Node[MAX_N];

void Input(){

	cin >> N;
	for (int i = 0; i < N - 1; i++){
		int x, y;
		cin >> x >> y;
		Edge[x].push_back(y);
		Edge[y].push_back(x);
	}

}

void RootToLeaf(int pos, int pre, int len){

	Node[pos].RootLength = len;
	bool leafFlag = true;
	
	for (int i = 0; i < Edge[pos].size(); i++){
		if (Edge[pos][i] == pre)
			continue;
		leafFlag = false;
		RootToLeaf(Edge[pos][i], pos, len + 1);
	}

	if (leafFlag){
		Node[pos].LeafFlag = true;
	}

}

void Calc(){

	//各ノードについて根からの距離を求める
	//ついでに葉かどうかも調べる
	RootToLeaf(1, -1, 0);

	//直近の葉からの距離を幅優先で調べる
	typedef pair<int, int> II;
	queue<II> que;
	for (int i = 1; i <= N; i++){
		if (Node[i].LeafFlag)
			que.push(II(i, 0));
	}

	while (que.size()){
	
		II p = que.front();
		que.pop();

		int pos = p.first;
		int len = p.second;

		if (Node[pos].LeafLength >= 0)
			continue;

		Node[pos].LeafLength = len;
		
		for (int i = 0; i < Edge[pos].size(); i++){
			if (Node[Edge[pos][i]].LeafLength == -1){
				que.push(II(Edge[pos][i], len + 1));
			}
		}
	
	}

}

void Solve(){

	for (int i = 1; i <= N; i++){
		cout << min(Node[i].RootLength, Node[i].LeafLength) << endl;
	}

}

int main(){

	Input();

	Calc();

	Solve();

	return 0;

}
0