結果

問題 No.277 根掘り葉掘り
ユーザー sugim48sugim48
提出日時 2015-09-04 22:29:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 238 ms / 3,000 ms
コード長 1,566 bytes
コンパイル時間 704 ms
コンパイル使用メモリ 91,660 KB
実行使用メモリ 19,196 KB
最終ジャッジ日時 2023-09-26 04:37:15
合計ジャッジ時間 4,477 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 238 ms
19,196 KB
testcase_10 AC 197 ms
10,128 KB
testcase_11 AC 218 ms
9,892 KB
testcase_12 AC 227 ms
14,340 KB
testcase_13 AC 222 ms
9,916 KB
testcase_14 AC 215 ms
10,220 KB
testcase_15 AC 219 ms
10,696 KB
testcase_16 AC 224 ms
10,176 KB
testcase_17 AC 215 ms
10,116 KB
testcase_18 AC 222 ms
10,120 KB
testcase_19 AC 220 ms
10,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-8;
int INF = INT_MAX / 2;

void dfs(int u, int p, vector<vector<int> >& G, vector<int>& a, vector<int>& b) {
	bool leaf = true;
	for (int i = 0; i < G[u].size(); i++) {
		int v = G[u][i];
		if (v == p) continue;
		leaf = false;
		a[v] = a[u] + 1;
		dfs(v, u, G, a, b);
		b[u] = min(b[u], b[v] + 1);
	}
	if (leaf) b[u] = 0;
}

void _dfs(int u, int p, vector<vector<int> >& G, vector<int>& a, vector<int>& b, vector<int>& c) {
	c[u] = min(c[u], min(a[u], b[u]));
	for (int i = 0; i < G[u].size(); i++) {
		int v = G[u][i];
		if (v == p) continue;
		c[v] = c[u] + 1;
		_dfs(v, u, G, a, b, c);
	}
}

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> a(N), b(N, INT_MAX), c(N);
	dfs(0, -1, G, a, b);
	_dfs(0, -1, G, a, b, c);
	for (int u = 0; u < N; u++)
		cout << c[u] << endl;
}
0