#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll LOG = 31;
const ll MOD = 1000000007;
const ll inf = 1e17;
const ll B = 2305843009213693951;
#define db(x) cerr << #x << " = " << x << " | "
#define dbg(x) cerr << #x << " = " << x << "\n"
#define Algerian ios::sync_with_stdio(0);
#define OI cin.tie(NULL);
int main() {
Algerian OI
ll n, k;
cin >> n >> k;
vector<ll> a(n), p(n);
vector<vector<ll>> ch(n);
for (ll i = 0; i < n; i++) cin >> a[i];
for (ll i = 0; i < n; i++) {
cin >> p[i];
if (p[i] != -1) ch[p[i]].push_back(i);
}
vector<vector<ll>> freq(n, vector<ll>(k));
vector<ll> dep(n, -1);
dep[0] = 0;
auto tmp = [&](ll x, auto&& tmp) -> ll {
return (dep[x] == -1 ? dep[x] = tmp(p[x], tmp) + 1 : dep[x]);
};
for (ll i = 0; i < n; i++) {
dep[i] = tmp(p[i], tmp);
freq[dep[i]][a[i]]++;
}
ll res = 0;
vector<ll> d(n);
auto dfs = [&](ll u, auto&& dfs) -> void {
d[dep[u]]++;
for (ll v : ch[u]) {
dfs(v, dfs);
}
};
for (ll i = 0; i < n; i++) {
for (ll& x : d) x = 0;
dfs(i, dfs);
ll h = 0;
for (ll j = 0; j < n; j++) {
h += min(freq[j][a[i]], d[i]);
}
res = max(res, h);
}
cout << res << "\n";
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |