#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<int> par, color;
vector<vector<int>> by_color, adj;
vector<int> depth, in, out;
vector<vector<int>> subtree_sz_by_depth;
int t = 0;
void dfs(int node) {
in[node] = t++;
for (auto v : adj[node]) {
if (v == par[node]) continue;
depth[v] = depth[node] + 1;
dfs(v);
}
out[node] = t;
}
// coders, -(num reorganization)
pair<int, int> opt;
vector<int> aux;
void dfs2(int node) {
for (auto v : adj[node]) {
if (v == par[node]) continue;
dfs2(v);
// add to subtree_sz_by_depth
if (subtree_sz_by_depth[v].size() >= subtree_sz_by_depth[node].size()) {
subtree_sz_by_depth[v].push_back(1);
subtree_sz_by_depth[node].pop_back();
subtree_sz_by_depth[v].swap(subtree_sz_by_depth[node]);
}
int offset = subtree_sz_by_depth[node].size() - subtree_sz_by_depth[v].size() - 1;
for (int i = 0; i < subtree_sz_by_depth[v].size(); ++i) {
subtree_sz_by_depth[node][i + offset] += subtree_sz_by_depth[v][i];
}
}
int num_reorgs = 0;
int num_value = 0;
for (auto v : by_color[color[node]]) {
// if not in child, then it's a reorganization
if (!(in[node] <= in[v] && in[v] < out[node])) {
num_reorgs += 1;
}
int index = subtree_sz_by_depth[node].size() - 1 - (depth[v] - depth[node]);
if (aux[index] < subtree_sz_by_depth[node][index]) {
aux[index] += 1;
num_value += 1;
} else if (in[node] <= in[v] && in[v] < out[node]) {
// remove a programmer from outside
num_reorgs -= 1;
}
}
opt = max(opt, {num_value, -num_reorgs});
for (auto v : by_color[color[node]]) {
int index = subtree_sz_by_depth[node].size() - 1 - (depth[v] - depth[node]);
aux[index] = 0;
}
}
void solve() {
cin >> n >> k;
color.assign(n, 0), adj.assign(n, {}), by_color.assign(k, {}), depth.assign(n, {});
in.assign(n, 0), out.assign(n, 0), subtree_sz_by_depth.assign(n, {1});
aux.assign(n, 0);
for (auto& c : color) cin >> c;
for (int i = 0; i < n; ++i) by_color[color[i]].push_back(i);
par.assign(n, -1);
for (int i = 1; i < n; ++i) {
cin >> par[i];
adj[par[i]].push_back(i);
}
dfs(0);
dfs2(0);
cout << opt.first << " " << -opt.second << "\n";
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
solve();
}
Compilation message
Main.cpp: In function 'void dfs2(int)':
Main.cpp:38:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for (int i = 0; i < subtree_sz_by_depth[v].size(); ++i) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:84:9: warning: unused variable 't' [-Wunused-variable]
84 | int t = 1;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
900 KB |
Output is correct |
9 |
Runtime error |
45 ms |
58720 KB |
Execution killed with signal 11 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
7 ms |
860 KB |
Output is correct |
5 |
Runtime error |
40 ms |
53592 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Runtime error |
48 ms |
58912 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
6 ms |
996 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Runtime error |
1 ms |
1628 KB |
Execution killed with signal 11 |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
900 KB |
Output is correct |
9 |
Runtime error |
45 ms |
58720 KB |
Execution killed with signal 11 |
10 |
Halted |
0 ms |
0 KB |
- |