結果

問題 No.277 根掘り葉掘り
ユーザー airisairis
提出日時 2015-09-04 23:12:22
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,304 bytes
コンパイル時間 670 ms
コンパイル使用メモリ 78,216 KB
実行使用メモリ 16,868 KB
最終ジャッジ日時 2023-09-26 06:24:12
合計ジャッジ時間 4,402 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,608 KB
testcase_01 AC 4 ms
6,424 KB
testcase_02 WA -
testcase_03 AC 5 ms
6,496 KB
testcase_04 AC 4 ms
6,780 KB
testcase_05 AC 5 ms
6,464 KB
testcase_06 AC 4 ms
6,536 KB
testcase_07 AC 4 ms
6,448 KB
testcase_08 AC 4 ms
6,476 KB
testcase_09 AC 226 ms
16,868 KB
testcase_10 AC 193 ms
10,376 KB
testcase_11 AC 215 ms
10,428 KB
testcase_12 AC 220 ms
13,672 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <numeric>
#include <bitset>
#include <complex>
#define rep(x, to) for (int x = 0; x < (to); x++)
#define REP(x, a, to) for (int x = (a); x < (to); x++)
#define foreach(itr, x) for (typeof((x).begin()) itr = (x).begin(); itr != (x).end(); itr++)
#define EPS (1e-14)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef complex<double> Complex;
typedef vector< vector<int> > Mat;

const ll INF = (ll)(1e+18);
const int MAX_V = 100005;

int N;
ll L[MAX_V], R[MAX_V];
vector<int> edge[MAX_V];

ll dfs(int u, int prev, ll depth) {
	R[u] = depth;
	rep(i, edge[u].size()) {
		int v = edge[u][i];
		if (v == prev) continue;
		L[u] = min(L[u], 1 + dfs(v, u, depth + 1LL));
	}
	if (INF == L[u]) L[u] = 0;
	return L[u];
}

void solve() {
	rep(i, MAX_V) L[i] = INF;
	dfs(1, -1, 0);
	rep(i, N) {
		cout << min(R[i + 1], L[i + 1]) << endl;
		//cout << R[i + 1] << " " <<  L[i + 1] << endl;
	}
}

int main() {
	cin >> N;
	rep(i, N - 1) {
		int x, y;
		cin>> x >> y;
		edge[x].push_back(y);
		edge[y].push_back(x);
	}
	solve();
	return 0;
}


0