#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];
bool chk[Nmax];
vector<int> adj[Nmax];
unordered_map<int, int> X[Nmax];
pair<unordered_map<int, int>, unordered_map<int, unordered_map<int, int>>> P[Nmax];
void DFS1(int curr) {
X[A[curr]][D[curr]]++;
for(int next:adj[curr]) chk[next]=(chk[curr] || (A[curr]==2)), D[next]=D[curr]+1, DFS1(next);
}
void DFS(int curr) {
pair<unordered_map<int, int>, unordered_map<int, unordered_map<int, int>>> &ret=P[curr];
ret.first[D[curr]]++, ret.second[A[curr]][D[curr]]++;
for(int next:adj[curr]) {
DFS(next);
pair<unordered_map<int, int>, unordered_map<int, unordered_map<int, int>>> &tmp=P[next];
if(ret.first.size()<tmp.first.size()) swap(ret, tmp);
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;
if(curr==1 || (A[curr]==2 && !chk[curr])) {
for(auto k:X[A[curr]]) sum+=min(X[A[curr]][k.first], ret.first[k.first]), val+=ret.second[A[curr]][k.first];
val=sum-val;
if(ans<sum) ans=sum, mn=val;
if(ans==sum && mn>val) mn=val;
}
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>N>>K;
for(int i=1; i<=N; i++) cin>>A[i], ++A[i];
if(A[1]==2) for(int i=1; i<=N; i++) A[i]=3-A[i];
for(int i=1; i<=N; 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 |
4 ms |
21084 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Incorrect |
3 ms |
21084 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
21084 KB |
Output is correct |
2 |
Correct |
5 ms |
21084 KB |
Output is correct |
3 |
Correct |
4 ms |
21080 KB |
Output is correct |
4 |
Correct |
6 ms |
22616 KB |
Output is correct |
5 |
Correct |
115 ms |
105836 KB |
Output is correct |
6 |
Correct |
4 ms |
21084 KB |
Output is correct |
7 |
Correct |
7 ms |
22108 KB |
Output is correct |
8 |
Correct |
131 ms |
73556 KB |
Output is correct |
9 |
Correct |
4 ms |
21340 KB |
Output is correct |
10 |
Correct |
186 ms |
113136 KB |
Output is correct |
11 |
Runtime error |
1218 ms |
1048576 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21084 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Incorrect |
4 ms |
21116 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21084 KB |
Output is correct |
2 |
Correct |
4 ms |
21084 KB |
Output is correct |
3 |
Incorrect |
3 ms |
21164 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21084 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Incorrect |
3 ms |
21084 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |