Submission #1211292

#TimeUsernameProblemLanguageResultExecution timeMemory
1211292trimkusTeam Coding (EGOI24_teamcoding)C++20
42 / 100
4057 ms1114112 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;


int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n, k;
	cin >> n >> k;
	vector<int> c(n);
	vector<vector<int>> adj(n);
	for (int i = 0; i < n; ++i) {
		cin >> c[i];
	}
	for (int i = 1; i < n; ++i) {
		int x;
		cin >> x;
		adj[x].push_back(i);
	}
	vector<bool> is_root(n);
	vector<vector<int>> col_adj(n);
	vector<int> depth(n), mx_depth(n);
	vector<map<int, int>> col_count(k);
	auto dfs = [&](auto& dfs, int i, vector<int>& parent) -> void {
		int prv_c = parent[c[i]];
		if (prv_c == -1) {
			is_root[i] = true;
		} else {
			col_adj[prv_c].push_back(i);
		}
		parent[c[i]] = i;
		mx_depth[i] = depth[i];
		col_count[c[i]][depth[i]]++;
		for (auto& u : adj[i]) {
			depth[u] = depth[i] + 1;
			dfs(dfs, u, parent);
			mx_depth[i] = max(mx_depth[i], mx_depth[u]);
		}
		parent[c[i]] = prv_c;
	};
	vector<int> parent(k, -1);
	dfs(dfs, 0, parent);
	//~ for (int i = 0; i < n; ++i) {
		//~ cout << is_root[i] << " ";
	//~ }
	//~ cout << "\n";
	vector<map<int, int>> counts(n);
	int res1 = 0, res2 = 0;
	auto dfs1 = [&](auto& dfs1, int i) -> void {
		counts[i][depth[i]] += 1;
		for (auto& u : adj[i]) {
			dfs1(dfs1, u);
			if (counts[i].size() < counts[u].size()) swap(counts[i], counts[u]);
			for (auto& [d, cnt] : counts[u]) {
				counts[i][d] += cnt;
			}
		}
		map<int, int> current_col;
		if (is_root[i]) {
			queue<int> q;
			q.push(i);
			while (q.size()) {
				int v = q.front();
				q.pop();
				current_col[depth[v]] += 1;
				for (auto& u : col_adj[v]) {
					q.push(u);
				}
			}
			int now1 = 0, now2 = 0;
			const int color = c[i];
			for (auto& [d, space] : counts[i]) {
				int can_get = min(space, col_count[color][d]);
				now1 += can_get;
				assert(can_get >= current_col[d]);
				now2 += can_get - current_col[d];
			}
			if (res1 < now1) {
				res1 = now1;
				res2 = now2;
			}
			if (res1 == now1) res2 = min(res2, now2);
		}
	};
	dfs1(dfs1, 0);
	cout << res1 << " " << res2 << "\n";
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...