#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
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});
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 |
344 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
37 ms |
31188 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
7 ms |
1008 KB |
Output is correct |
12 |
Execution timed out |
4059 ms |
28504 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 |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
860 KB |
Output is correct |
5 |
Execution timed out |
4099 ms |
28532 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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 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 |
54 ms |
31272 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
1016 KB |
Output is correct |
11 |
Correct |
43 ms |
32796 KB |
Output is correct |
12 |
Correct |
0 ms |
352 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 |
608 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
67 ms |
15956 KB |
Output is correct |
19 |
Correct |
65 ms |
15452 KB |
Output is correct |
20 |
Correct |
75 ms |
17576 KB |
Output is correct |
21 |
Correct |
47 ms |
12888 KB |
Output is correct |
22 |
Correct |
60 ms |
15764 KB |
Output is correct |
23 |
Correct |
82 ms |
24024 KB |
Output is correct |
24 |
Correct |
42 ms |
20812 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
1112 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
3 ms |
1884 KB |
Output is correct |
30 |
Correct |
64 ms |
15072 KB |
Output is correct |
31 |
Correct |
70 ms |
16984 KB |
Output is correct |
32 |
Correct |
82 ms |
20572 KB |
Output is correct |
33 |
Correct |
36 ms |
15184 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 |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
7 ms |
1024 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
37 ms |
31188 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
7 ms |
1008 KB |
Output is correct |
12 |
Execution timed out |
4059 ms |
28504 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |