#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 |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
50 ms |
22292 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
71 ms |
26288 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
45 ms |
19896 KB |
Output is correct |
12 |
Correct |
29 ms |
30424 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
45 ms |
8608 KB |
Output is correct |
16 |
Correct |
24 ms |
8484 KB |
Output is correct |
17 |
Correct |
0 ms |
456 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
40 ms |
8644 KB |
Output is correct |
20 |
Correct |
35 ms |
8452 KB |
Output is correct |
21 |
Correct |
25 ms |
8592 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |