結果

問題 No.277 根掘り葉掘り
ユーザー omuomu
提出日時 2015-09-04 23:08:01
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 268 ms / 3,000 ms
コード長 1,969 bytes
コンパイル時間 699 ms
コンパイル使用メモリ 94,920 KB
実行使用メモリ 12,688 KB
最終ジャッジ日時 2023-09-26 06:21:34
合計ジャッジ時間 4,849 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,492 KB
testcase_01 AC 4 ms
6,584 KB
testcase_02 AC 4 ms
6,432 KB
testcase_03 AC 4 ms
6,752 KB
testcase_04 AC 4 ms
6,636 KB
testcase_05 AC 4 ms
6,608 KB
testcase_06 AC 5 ms
6,564 KB
testcase_07 AC 4 ms
6,748 KB
testcase_08 AC 4 ms
6,604 KB
testcase_09 AC 227 ms
12,688 KB
testcase_10 AC 200 ms
9,624 KB
testcase_11 AC 236 ms
9,656 KB
testcase_12 AC 268 ms
11,316 KB
testcase_13 AC 262 ms
9,844 KB
testcase_14 AC 252 ms
10,156 KB
testcase_15 AC 256 ms
10,052 KB
testcase_16 AC 257 ms
9,940 KB
testcase_17 AC 252 ms
9,864 KB
testcase_18 AC 248 ms
9,792 KB
testcase_19 AC 248 ms
9,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <functional>
#include <queue>
#include <stack>
#include <cmath>
#include <iomanip>
#include <list>
#include <tuple>
#include <bitset>
#include <ciso646>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
typedef vector<ll> vec;

inline bool cheak(ll x, ll y, ll xMax, ll yMax){ return x >= 0 && y >= 0 && xMax > x && yMax > y; }
inline ll toInt(string s) { ll v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }
template<class T> inline T sqr(T x) { return x*x; }

#define For(i,a,b)	for(ll (i) = (a);i < (b);(i)++)
#define rep(i,n)	For(i,0,n)
#define rFor(i,a,b)	for(ll (i) = (a-1);i >= (b);(i)--)
#define rrep(i,n)	rFor(i,n,0)
#define clr(a)		memset((a), 0 ,sizeof(a))
#define mclr(a)		memset((a), -1 ,sizeof(a))
#define all(a)		(a).begin(),(a).end()
#define sz(a)		(sizeof(a))

const ll mod = 1e9 + 7;
const ll INF = 1e9 + 9;

vector<int> to[100001];
int root[100001];
int leaf[100001];

int n;
void root_dfs(int m,int t){
	if (t >= root[m])return;
	root[m] = min(t, root[m]);
	rep(i, to[m].size()){
		root_dfs(to[m][i],t+1);
	}
}
void leaf_dfs(int m, int t){
	if (t >= leaf[m])return;
	leaf[m] = min(t, leaf[m]);
	rep(i, to[m].size()){
		leaf_dfs(to[m][i], t + 1);
	}
}

int main()
{
	fill(root, root + 100001, INF);
	fill(leaf, leaf + 100001, INF);

	cin >> n;
	rep(i, n-1){
		int x, y;
		cin >> x >> y;
		to[x].push_back(y);
		to[y].push_back(x);
	}

	//根の最小値の更新
	root_dfs(1, 0);
	//葉の最小値の更新
	For(i,2, n+1){
		if (to[i].size() == 1){
			leaf_dfs(i, 0);
		}
	}
	For(i, 1, n + 1){
		cout << min(root[i], leaf[i]) << endl;
	}

	return 0;
}
0