제출 #1341115

#제출 시각아이디문제언어결과실행 시간메모리
1341115aykhnTeam Coding (EGOI24_teamcoding)C++20
100 / 100
1289 ms907016 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
 
using namespace __gnu_pbds;
using namespace std;

#define inf 0x3F3F3F3F3F3F3F3F
#define bpc __builtin_popcountll

const int mod = 998244353;
const int MXN = 2e5 + 5;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();

struct chash {
    int operator()(int x) const { return x ^ RANDOM; }
};

int n, k;
vector<int> adj[MXN];
int val[MXN], sz[MXN], dep[MXN], freq[MXN];
gp_hash_table<int, int, chash> mp[MXN], d[MXN];
gp_hash_table<int, gp_hash_table<int, int, chash>, chash> mp1[MXN];

array<int, 2> res = {0, 0};

void dfs(int a)
{
	sz[a] = 1;
	freq[val[a]]++;
	for (int &v : adj[a])
	{
		dfs(v);
		if (sz[a] < sz[v]) mp[a].swap(mp[v]), mp1[a].swap(mp1[v]);
		for (auto it = mp1[v].begin(); it != mp1[v].end(); it++)
		{
			int key = (*it).first;
			for (pair<int, int> p : (*it).second) mp1[a][key][p.first] += p.second;
		}
		for (auto &p : mp[v]) mp[a][p.first] += p.second;
		sz[a] += sz[v];
	}
	freq[val[a]]--;
	mp[a][dep[a]]++;
	mp1[a][val[a]][dep[a]]++;
	if (freq[val[a]]) return;
	gp_hash_table<int, int, chash> &mp2 = mp1[a][val[a]];
	array<int, 2> ans = {0, 0};
    if (d[val[a]].size() < mp[a].size())
	{
		for (auto &p : d[val[a]]) ans[0] += min(p.second, mp[a][p.first]), ans[1] += min(p.second, mp[a][p.first]) - mp2[p.first];
	}
	else
	{
		for (auto &p : mp[a]) ans[0] += min(p.second, d[val[a]][p.first]), ans[1] += min(p.second, d[val[a]][p.first]) - mp2[p.first];
	}
	if (ans[0] > res[0]) res = ans;
	else if (ans[0] == res[0]) res[1] = min(res[1], ans[1]);
}

void _init(int a)
{
	d[val[a]][dep[a]]++;
	for (int &v : adj[a])
	{
		dep[v] = dep[a] + 1;
		_init(v);
	}
}

signed main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> k;
	for (int i = 0; i < n; i++) cin >> val[i];
	for (int i = 1; i < n; i++)
	{
		int p;
		cin >> p;
		adj[p].push_back(i);
	}
	_init(0);
	dfs(0);
	cout << res[0] << ' ' << res[1] << '\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...