#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) {
P[curr].first[D[curr]]++, P[curr].second[A[curr]][D[curr]]++;
for(int next:adj[curr]) {
DFS(next);
if(P[curr].first.size()<P[next].first.size()) swap(P[curr], P[next]);
for(auto k:P[next].first) P[curr].first[k.first]+=k.second;
for(auto k:P[next].second) for(auto kk:k.second) P[curr].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], P[curr].first[k.first]), val+=P[curr].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 |
4 ms |
21084 KB |
Output is correct |
3 |
Incorrect |
4 ms |
21084 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 |
Correct |
3 ms |
21236 KB |
Output is correct |
4 |
Correct |
5 ms |
22620 KB |
Output is correct |
5 |
Correct |
166 ms |
107444 KB |
Output is correct |
6 |
Correct |
4 ms |
21188 KB |
Output is correct |
7 |
Correct |
5 ms |
22176 KB |
Output is correct |
8 |
Correct |
135 ms |
73520 KB |
Output is correct |
9 |
Correct |
4 ms |
21336 KB |
Output is correct |
10 |
Correct |
176 ms |
113120 KB |
Output is correct |
11 |
Runtime error |
1194 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 |
3 ms |
21084 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21080 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 |
4 ms |
21084 KB |
Output is correct |
2 |
Correct |
4 ms |
21084 KB |
Output is correct |
3 |
Incorrect |
4 ms |
21084 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |