#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 = 1;
aux[subtree_sz_by_depth[node].size() - 1] = 1;
for (auto v : by_color[color[node]]) {
if (depth[v] <= depth[node] || depth[v] >= depth[node] + subtree_sz_by_depth[node].size()) continue;
// if not in child, then it's a reorganization
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;
if (!(in[node] <= in[v] && in[v] < out[node])) {
num_reorgs += 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});
aux[subtree_sz_by_depth[node].size() - 1] = 0;
for (auto v : by_color[color[node]]) {
if (depth[v] <= depth[node] || depth[v] >= depth[node] + subtree_sz_by_depth[node].size()) continue;
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:46:49: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | if (depth[v] <= depth[node] || depth[v] >= depth[node] + subtree_sz_by_depth[node].size()) continue;
Main.cpp:63:49: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | if (depth[v] <= depth[node] || depth[v] >= depth[node] + subtree_sz_by_depth[node].size()) continue;
Main.cpp: In function 'int main()':
Main.cpp:88:9: warning: unused variable 't' [-Wunused-variable]
88 | 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 |
344 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
39 ms |
31196 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
860 KB |
Output is correct |
12 |
Execution timed out |
4097 ms |
28616 KB |
Time limit exceeded |
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 |
5 ms |
860 KB |
Output is correct |
5 |
Execution timed out |
4099 ms |
28508 KB |
Time limit exceeded |
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 |
1 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 |
49 ms |
31300 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
46 ms |
32708 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
61 ms |
15952 KB |
Output is correct |
19 |
Correct |
70 ms |
15444 KB |
Output is correct |
20 |
Correct |
80 ms |
17748 KB |
Output is correct |
21 |
Correct |
63 ms |
12860 KB |
Output is correct |
22 |
Correct |
50 ms |
15688 KB |
Output is correct |
23 |
Correct |
53 ms |
24024 KB |
Output is correct |
24 |
Correct |
53 ms |
20888 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
860 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
4 ms |
1884 KB |
Output is correct |
30 |
Correct |
58 ms |
14928 KB |
Output is correct |
31 |
Correct |
65 ms |
16980 KB |
Output is correct |
32 |
Correct |
61 ms |
20560 KB |
Output is correct |
33 |
Correct |
45 ms |
15424 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
352 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
0 ms |
352 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 |
352 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 |
5 ms |
860 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
11 ms |
700 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
16 ms |
708 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
860 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
5 ms |
920 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
11 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
3 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
2 ms |
600 KB |
Output is correct |
37 |
Correct |
12 ms |
604 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
39 ms |
31196 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
860 KB |
Output is correct |
12 |
Execution timed out |
4097 ms |
28616 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |