#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;
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], 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 |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Runtime error |
6 ms |
17500 KB |
Execution killed with signal 8 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
60 ms |
10588 KB |
Output is correct |
5 |
Execution timed out |
4090 ms |
97992 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 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 |
Runtime error |
6 ms |
17500 KB |
Execution killed with signal 8 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Runtime error |
5 ms |
17500 KB |
Execution killed with signal 8 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Runtime error |
6 ms |
17500 KB |
Execution killed with signal 8 |
7 |
Halted |
0 ms |
0 KB |
- |