#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];
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);
}
pair<unordered_map<int, int>, unordered_map<int, unordered_map<int, int>>> DFS(int curr) {
pair<unordered_map<int, int>, unordered_map<int, unordered_map<int, int>>> ret;
ret.first[D[curr]]++, ret.second[A[curr]][D[curr]]++;
for(int next:adj[curr]) {
pair<unordered_map<int, int>, unordered_map<int, unordered_map<int, int>>> tmp=DFS(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;
}
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], ++A[i];
if(A[1]==2) for(int i=1; i<=N; i++) A[i]=3-A[i], C[A[i]]++;
else 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 |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Incorrect |
1 ms |
8796 KB |
Output isn't correct |
4 |
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 |
4 ms |
10588 KB |
Output is correct |
5 |
Correct |
112 ms |
101004 KB |
Output is correct |
6 |
Correct |
2 ms |
8792 KB |
Output is correct |
7 |
Correct |
3 ms |
8792 KB |
Output is correct |
8 |
Correct |
35 ms |
11692 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
167 ms |
51452 KB |
Output is correct |
11 |
Runtime error |
1192 ms |
1048576 KB |
Execution killed with signal 9 |
12 |
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 |
Incorrect |
1 ms |
8796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Incorrect |
1 ms |
8796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Incorrect |
1 ms |
8796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |