#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);
	vector<int> tot(k);
	for (int i = 0; i < n; ++i) {
		cin >> c[i];
		tot[c[i]] += 1;
	}
	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<unordered_map<int, int>> col_count(k);
	vector<vector<int>> diff_depths(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]]++;
		diff_depths[c[i]].push_back(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 < k; ++i) {
		auto& v = diff_depths[i];
		sort(begin(v), end(v));
		v.erase(unique(begin(v), end(v)), end(v));
	}
	vector<map<int, int>> counts(n);
	map<int, int> current_col;
	int res1 = 0, res2 = 0;
	const int M = sqrt(n);
	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;
			}
			counts[u].clear();
		}
		current_col.clear();
		if (is_root[i] && tot[c[i]] <= M) {
			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 : diff_depths[color]) {
				if (counts[i].count(d)) {
					int space = counts[i][d];
					int can_get = min(space, col_count[color][d]);
					now1 += can_get;
					now2 += can_get - current_col[d];
				}
			}
			if (res1 < now1) {
				res1 = now1;
				res2 = now2;
			}
			if (res1 == now1) res2 = min(res2, now2);
		}
	};
	dfs1(dfs1, 0);
	vector<vector<int>> roots(k);
	auto dfs2 = [&](auto& dfs2, int i, vector<int>& cnt, vector<int>& cur_col_cnt, int d, const int need) -> void {
		while (d >= (int)cnt.size()) cnt.push_back(0);
		while (d >= (int)cur_col_cnt.size()) cur_col_cnt.push_back(0);
		cnt[d] += 1;
		if (c[i] == need) cur_col_cnt[d] += 1;
		for (auto& u : adj[i]) {
			dfs2(dfs2, u, cnt, cur_col_cnt, d + 1, need);
		}
	};
	for (int i = 0; i < n; ++i) {
		if (is_root[i] && tot[c[i]] > M) {
			vector<int> cnt, cur_col_cnt;
			dfs2(dfs2, i, cnt, cur_col_cnt, 0, c[i]);
			cnt.push_back(0);
			int d = 0;
			int now1 = 0, now2 = 0;
			while (cnt[d] > 0) {
				int true_d = d + depth[i];
				if (col_count[c[i]].count(true_d)) {
					int can_get = min(cnt[d], col_count[c[i]][true_d]);
					now1 += can_get;
					now2 += can_get - cur_col_cnt[d];
				}
				d += 1;
			}
			if (res1 < now1) {
				res1 = now1;
				res2 = now2;
			}
			if (res1 == now1) res2 = min(res2, now2);
		}
	}
	cout << res1 << " " << res2 << "\n";
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |