#include <bits/stdc++.h>
using namespace std;
#define iter(v) v.begin(), v.end()
#define pb emplace_back
#define ff first
#define ss second
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#ifdef zisk
void debug() { cerr << "\n"; }
template<class T, class ... U>
void debug(T a, U... b) {
cerr << a << " ", debug(b...);
}
template<class T>
void pary(T l, T r){
while (l != r) cerr << *l << " ", l++;
cerr << "\n";
}
#else
#define debug(...) void()
#define pary(...) void()
#endif
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, K;
cin >> n >> K;
vector<int> color(n);
for (int i = 0; i < n; i++) cin >> color[i];
vector<vector<int>> g(n);
for (int i = 1; i < n; i++) {
int p;
cin >> p;
g[p].pb(i);
}
vector<int> dpt(n);
vector<int> cur(K, -1);
vector<bool> top(n);
auto dfs1 = [&](auto dfs, int now) -> void {
if (cur[color[now]] == -1) {
cur[color[now]] = now;
top[now] = true;
}
for (int i : g[now]) {
dpt[i] = dpt[now] + 1;
dfs(dfs, i);
}
if (cur[color[now]] == now)
cur[color[now]] = -1;
};
dfs1(dfs1, 0);
int ans_member = 0, ans_swp = 0;
auto solve_big = [&](int c) {
//debug("solve_big", c);
vector<int> total(n + 1);
for (int i = 0; i < n; i++)
if (color[i] == c) total[dpt[i]]++;
vector<int> cnt(n + 1), sz(n + 1);
auto dfs2 = [&](auto dfs, int now, int type) -> void {
sz[dpt[now]] += type;
if (color[now] == c) cnt[dpt[now]] += type;
for (int i : g[now]) dfs(dfs, i, type);
};
for (int i = 0; i < n; i++)
if (color[i] == c && top[i]) {
dfs2(dfs2, i, 1);
int member = 0, swp = 0;
for (int j = dpt[i]; j <= n && sz[j]; j++) {
//debug("depth", j, sz[j], cnt[j], total[j]);
int tmp = min(total[j], sz[j]);
member += tmp;
swp += tmp - cnt[j];
}
//debug("top", i, member, swp);
if (member > ans_member) ans_member = member, ans_swp = swp;
else if (member == ans_member && swp < ans_swp) ans_swp = swp;
dfs2(dfs2, i, -1);
}
};
for (int i = 0; i < K; i++) solve_big(i);
cout << ans_member << " " << ans_swp << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
1 ms |
604 KB |
Output is correct |
9 |
Execution timed out |
4097 ms |
14364 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
604 KB |
Output is correct |
5 |
Correct |
26 ms |
13520 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
26 ms |
7048 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
30 ms |
9564 KB |
Output is correct |
12 |
Correct |
17 ms |
5596 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
39 ms |
7184 KB |
Output is correct |
16 |
Correct |
36 ms |
7100 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
31 ms |
7120 KB |
Output is correct |
20 |
Correct |
34 ms |
7116 KB |
Output is correct |
21 |
Correct |
35 ms |
7108 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
Execution timed out |
4094 ms |
14212 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
29 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
5 ms |
560 KB |
Output is correct |
24 |
Correct |
4 ms |
348 KB |
Output is correct |
25 |
Correct |
6 ms |
540 KB |
Output is correct |
26 |
Correct |
18 ms |
604 KB |
Output is correct |
27 |
Correct |
25 ms |
604 KB |
Output is correct |
28 |
Correct |
10 ms |
348 KB |
Output is correct |
29 |
Correct |
7 ms |
604 KB |
Output is correct |
30 |
Correct |
24 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
472 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
4 ms |
604 KB |
Output is correct |
34 |
Correct |
6 ms |
580 KB |
Output is correct |
35 |
Correct |
7 ms |
588 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
10 ms |
596 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
344 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 |
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 |
1 ms |
604 KB |
Output is correct |
9 |
Execution timed out |
4097 ms |
14364 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |