結果

問題 No.1718 Random Squirrel
ユーザー nok0nok0
提出日時 2021-08-04 11:50:33
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 153 ms / 2,000 ms
コード長 1,976 bytes
コンパイル時間 2,281 ms
コンパイル使用メモリ 214,956 KB
実行使用メモリ 24,436 KB
最終ジャッジ日時 2023-10-24 11:23:45
合計ジャッジ時間 7,992 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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 1 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 AC 69 ms
9,548 KB
testcase_12 AC 17 ms
4,368 KB
testcase_13 AC 54 ms
7,008 KB
testcase_14 AC 77 ms
9,768 KB
testcase_15 AC 88 ms
8,592 KB
testcase_16 AC 38 ms
5,952 KB
testcase_17 AC 66 ms
9,256 KB
testcase_18 AC 121 ms
10,624 KB
testcase_19 AC 81 ms
8,328 KB
testcase_20 AC 25 ms
5,688 KB
testcase_21 AC 105 ms
12,048 KB
testcase_22 AC 150 ms
12,100 KB
testcase_23 AC 113 ms
12,056 KB
testcase_24 AC 107 ms
12,052 KB
testcase_25 AC 153 ms
12,108 KB
testcase_26 AC 66 ms
12,332 KB
testcase_27 AC 68 ms
12,336 KB
testcase_28 AC 67 ms
12,332 KB
testcase_29 AC 87 ms
18,184 KB
testcase_30 AC 94 ms
24,436 KB
testcase_31 AC 91 ms
24,436 KB
testcase_32 AC 87 ms
18,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
	int n, k;
	cin >> n >> k;

	vector<vector<int>> g(n), h(n);
	for(int i = 0; i < n - 1; i++) {
		int u, v;
		cin >> u >> v;
		--u, --v;
		g[u].emplace_back(v);
		g[v].emplace_back(u);
	}

	vector<bool> has_acorn(n);
	int root = -1;
	for(int i = 0; i < k; i++) {
		int a;
		cin >> a;
		has_acorn[--a] = 1;
		root = a;
	}

	vector<bool> in_tree(n);
	auto dfs = [&](auto dfs, int now, int per) -> bool {
		bool f = has_acorn[now];
		for(auto to : g[now]) {
			if(to == per) continue;
			f |= dfs(dfs, to, now);
		}
		return in_tree[now] = f;
	};

	dfs(dfs, root, -1);

	int es = 0;
	for(int i = 0; i < n; i++) {
		for(auto to : g[i]) {
			if(in_tree[i] and in_tree[to]) {
				es++;
			}
		}
	}

	auto height = [&]() -> vector<int> {
		auto bfs = [&](int s) {
			vector<int> dist(n, 1 << 30);
			queue<int> que;
			dist[s] = 0;
			que.push(s);
			while(!que.empty()) {
				int now = que.front();
				que.pop();
				for(auto to : g[now]) {
					if(!in_tree[to] or dist[to] != 1 << 30) continue;
					dist[to] = dist[now] + 1;
					que.push(to);
				}
			}
			return dist;
		};
		auto vec1 = bfs(root);
		int v1 = -1, v2 = -1;
		int dia = -1;
		for(int i = 0; i < n; i++)
			if(dia < vec1[i] and in_tree[i]) dia = vec1[i], v1 = i;
		vec1 = bfs(v1);
		dia = -1;
		for(int i = 0; i < n; i++)
			if(dia < vec1[i] and in_tree[i]) dia = vec1[i], v2 = i;
		auto vec2 = bfs(v2);
		for(int i = 0; i < n; i++)
			if(vec1[i] < vec2[i]) vec1[i] = vec2[i];
		return vec1;
	};

	auto hei = height();

	vector<int> res(n);
	for(int i = 0; i < n; i++) {
		if(in_tree[i]) {
			res[i] = es - hei[i];
			auto dfs = [&](auto dfs, int now, int per, int val) -> void {
				res[now] = val;
				for(auto to : g[now]) {
					if(to == per) continue;
					dfs(dfs, to, now, val + 1);
				}
			};
			for(auto to : g[i]) {
				if(!in_tree[to]) {
					dfs(dfs, to, i, res[i] + 1);
				}
			}
		}
	}

	for(auto v : res) cout << v << '\n';

	return 0;
}
0