This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ll n, k;
cin >> n >> k;
vector<ll> col(n);
vector<vector<ll>> adj(n);
vector<ll> depth(n);
vector<ll> team(n, -1);
pair<ll, ll> res = {0, 0};
for (int i = 0; i < n; ++i) {
cin >> col[i];
if (col[i] == col[0]) res.first++;
}
for (int i = 0; i < n - 1; ++i) {
ll p;
cin >> p;
adj[p].push_back(i + 1);
}
queue<ll> q;
q.push(0);
depth[0] = 0;
ll t = 0;
while (!q.empty()) {
ll curr = q.front();
q.pop();
for (ll v : adj[curr]) {
depth[v] = depth[curr] + 1;
if (team[curr] == -1 && col[v] != col[0]) team[v] = t++;
else team[v] = team[curr];
q.push(v);
}
}
vector<ll> numPerLayer(n, 0);
vector<map<ll, ll>> vPerTeamPerLayer(t);
vector<map<ll, ll>> vColPerTeamPerLayer(t);
for (int i = 0; i < n; ++i) {
if (col[i] != col[0]) {
numPerLayer[depth[i]]++;
vColPerTeamPerLayer[team[i]][depth[i]]++;
}
if (team[i] != -1) vPerTeamPerLayer[team[i]][depth[i]]++;
}
vector<pair<ll, ll>> resTeam(t, {0, 0});
for (int i = 0; i < t; ++i) {
for (auto& [d, k] : vPerTeamPerLayer[i]) {
resTeam[i].first += min(numPerLayer[d], k);
resTeam[i].second += min(numPerLayer[d], k) - vColPerTeamPerLayer[i][d];
}
}
for (int i = 0; i < t; ++i) {
if (resTeam[i].first > res.first) res = resTeam[i];
else if (resTeam[i].first == res.first && resTeam[i].second > res.second) res = resTeam[i];
}
cout << res.first << " " << res.second << endl;
}
# | 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... |