Submission #1148374

#TimeUsernameProblemLanguageResultExecution timeMemory
1148374AbdullahIshfaqTeam Coding (EGOI24_teamcoding)C++20
0 / 100
4 ms1608 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 998244353
const int N = 10000;
vector<ll> g[N];
int c[N], dep[N], root[N];
vector<int> colPar,  same, siz[N];
map<int,int> col_at_lev[N];
pair<ll,ll> best = {1,1};
void dfs(int x){
	int cp = colPar[c[x]];
	colPar[c[x]] = x;
	for(auto child : g[x]) {
		dep[child] = dep[x] + 1;
		dfs(child);
	}
	colPar[c[x]] = cp;
	if(cp == -1){
		root[x] = 1;
	}
	else{
		same[cp] += same[x];
	}
}
void dfs2(int x){
	for(auto child : g[x]) {
		dfs2(child);
		if(siz[child].size() > siz[x].size()){
			swap(siz[x], siz[child]);
		}
		for(int i = 0; i < siz[child].size(); i ++){
			siz[x][siz[x].size() - i - 1] += siz[child][siz[child].size() - i - 1];
		}
	}
	siz[x].push_back(1);
	if(!root[x]){
		return;
	}
	int ans = 1;
	for(auto it = col_at_lev[c[x]].upper_bound(dep[x]); it != col_at_lev[c[x]].end(); it++) {
		auto [lev, cnt] = *it;
		int ind = siz[x].size() - (lev - dep[x]) - 1;
		ans += min(siz[x][ind], cnt);
	}
	best = max(best, {ans, same[x]});
}
void solve(){
	ll n, k, u;
	cin >> n >> k;
	for(int i = 0; i < n ; i++){
		cin >> c[i];
	}
	for(int i = 1; i < n ; i++){
		cin >> u;
		g[u].push_back(i);
	}
	colPar.resize(n, -1);
	same.resize(n, 1);
	dfs(0);
 	for(int i = 0 ; i < n; i++){
		col_at_lev[c[i]][dep[i]]++;
	}
	dfs2(0);
	cout << best.first << " " << best.first-best.second << endl;
}
int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int tests = 1;
	// cin >> tests;
	for(int i = 1; i <= tests; i ++)
		solve();
}
#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...