結果

問題 No.1718 Random Squirrel
ユーザー square1001square1001
提出日時 2021-10-22 21:56:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,785 bytes
コンパイル時間 1,028 ms
コンパイル使用メモリ 89,448 KB
実行使用メモリ 20,496 KB
最終ジャッジ日時 2023-10-24 13:02:43
合計ジャッジ時間 6,428 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 WA -
testcase_12 AC 32 ms
4,468 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 152 ms
8,352 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 207 ms
10,200 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 252 ms
11,784 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 251 ms
11,784 KB
testcase_26 AC 186 ms
11,556 KB
testcase_27 WA -
testcase_28 AC 188 ms
11,556 KB
testcase_29 AC 206 ms
15,744 KB
testcase_30 AC 214 ms
20,496 KB
testcase_31 AC 211 ms
20,496 KB
testcase_32 AC 206 ms
15,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
int main() {
	int N, K;
	cin >> N >> K;
	vector<vector<int> > G(N);
	for (int i = 0; i < N - 1; ++i) {
		int a, b;
		cin >> a >> b; --a, --b;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	vector<int> D(K);
	for (int i = 0; i < K; ++i) {
		cin >> D[i]; --D[i];
	}
	vector<bool> donguri(N, false);
	for (int i = 0; i < K; ++i) {
		donguri[D[i]] = true;
	}
	vector<int> subsize(N);
	vector<int> overway(N, -1), depth(N);
	function<void(int, int, int)> dfs = [&](int pos, int pre, int id) {
		if (donguri[pos]) {
			subsize[pos] = 1;
			id = pos;
		}
		overway[pos] = id;
		for (int i : G[pos]) {
			if (i == pre) continue;
			depth[i] = depth[pos] + 1;
			dfs(i, pos, id);
			subsize[pos] += subsize[i];
		}
	};
	dfs(D[0], -1, D[0]);
	function<vector<int>(int)> shortest_path = [&](int pos) {
		queue<int> que;
		que.push(pos);
		vector<int> resdist(N, -1);
		resdist[pos] = 0;
		while (!que.empty()) {
			int u = que.front(); que.pop();
			for (int i : G[u]) {
				if (resdist[i] == -1 && subsize[i] >= 1) {
					resdist[i] = resdist[u] + 1;
					que.push(i);
				}
			}
		}
		return resdist;
	};
	vector<int> v0 = shortest_path(D[0]);
	int p1 = max_element(v0.begin(), v0.end()) - v0.begin();
	vector<int> v1 = shortest_path(p1);
	int p2 = max_element(v1.begin(), v1.end()) - v1.begin();
	vector<int> v2 = shortest_path(p2);
	int base_cost = -2;
	for (int i = 0; i < N; ++i) {
		if (subsize[i] >= 1) {
			base_cost += 2;
		}
	}
	for (int i = 0; i < N; ++i) {
		if (subsize[i] >= 1) {
			cout << base_cost - max(v1[i], v2[i]) << endl;
		}
		else {
			cout << base_cost + (depth[i] - depth[overway[i]]) - max(v1[overway[i]], v2[overway[i]]) << endl;
		}
	}
	return 0;
}
0