結果

問題 No.1718 Random Squirrel
ユーザー nok0nok0
提出日時 2021-08-04 11:51:31
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 1,973 bytes
コンパイル時間 2,310 ms
コンパイル使用メモリ 214,940 KB
実行使用メモリ 22,084 KB
最終ジャッジ日時 2023-10-24 11:23:51
合計ジャッジ時間 5,909 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 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 73 ms
7,900 KB
testcase_12 AC 17 ms
4,348 KB
testcase_13 AC 54 ms
5,984 KB
testcase_14 AC 76 ms
8,056 KB
testcase_15 AC 86 ms
7,264 KB
testcase_16 AC 37 ms
5,152 KB
testcase_17 AC 67 ms
7,688 KB
testcase_18 AC 122 ms
8,684 KB
testcase_19 AC 81 ms
7,000 KB
testcase_20 AC 25 ms
4,888 KB
testcase_21 AC 111 ms
9,696 KB
testcase_22 AC 154 ms
9,748 KB
testcase_23 AC 111 ms
9,704 KB
testcase_24 AC 105 ms
9,700 KB
testcase_25 AC 151 ms
9,756 KB
testcase_26 AC 65 ms
9,980 KB
testcase_27 AC 66 ms
9,984 KB
testcase_28 AC 65 ms
9,980 KB
testcase_29 AC 85 ms
15,832 KB
testcase_30 AC 92 ms
22,084 KB
testcase_31 AC 89 ms
22,084 KB
testcase_32 AC 85 ms
15,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
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 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