#include <bits/stdc++.h>
#define int long long
using namespace std;
const int Nmax=100010;
int N, K, ans, mn, A[Nmax], C[Nmax], D[Nmax];
vector<int> adj[Nmax];
map<int, int> X[Nmax];
void DFS1(int curr) {
X[A[curr]][D[curr]]++;
for(int next:adj[curr]) D[next]=D[curr]+1, DFS1(next);
}
pair<map<int, int>, map<int, map<int, int>>> DFS(int curr) {
pair<map<int, int>, map<int, map<int, int>>> ret;
ret.first[D[curr]]++, ret.second[A[curr]][D[curr]]++;
for(int next:adj[curr]) {
pair<map<int, int>, map<int, map<int, int>>> tmp=DFS(next);
for(auto k:tmp.first) ret.first[k.first]+=k.second;
for(auto k:tmp.second) for(auto kk:k.second) ret.second[k.first][kk.first]+=kk.second;
}
int sum=0, val=0;
for(auto k:ret.first) sum+=min(X[A[curr]][k.first], k.second);
for(auto k:ret.first) val+=min(X[A[curr]][k.first]-ret.second[A[curr]][k.first], k.second-ret.second[A[curr]][k.first]);
if(ans<sum) ans=sum, mn=val;
if(ans==sum && mn>val) mn=val;
return ret;
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>N>>K;
for(int i=1; i<=N; i++) cin>>A[i], C[++A[i]]++;
for(int i=2; i<=N; i++) {
int p; cin>>p; adj[++p].push_back(i);
}
DFS1(1);
DFS(1);
cout<<ans<<" "<<mn;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
12 ms |
9564 KB |
Output is correct |
8 |
Execution timed out |
4099 ms |
18988 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
802 ms |
10688 KB |
Output is correct |
5 |
Execution timed out |
4030 ms |
82768 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8792 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
11 ms |
9564 KB |
Output is correct |
8 |
Execution timed out |
4096 ms |
116684 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8612 KB |
Output is correct |
7 |
Correct |
12 ms |
9552 KB |
Output is correct |
8 |
Execution timed out |
4049 ms |
18556 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
12 ms |
9564 KB |
Output is correct |
8 |
Execution timed out |
4099 ms |
18988 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |