This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(),x.end()
int n,r;
vector<vector<int>> adj;
vector<multiset<int>> v;
vector<int> dp;
void dfs(int k,int d){
int big=n;
for(int j:adj[k]){
dfs(j,d+1);
if(v[big].size()<v[j].size()){
big=j;
}
dp[k]+=dp[j];
}
swap(v[k],v[big]);
for(int j:adj[k]){
if(j==big){
continue;
}
for(int i:v[j]){
v[k].insert(i);
}
}
while(v[k].size()){
int a=*v[k].rbegin();
if(a>=d+r){
dp[k]++;
v[k].erase(v[k].find(a));
}
else{
break;
}
}
while(v[k].size()>=2){
int a,b;
auto it=v[k].begin();
a=*it;
it++;
b=*it;
if(a+b-d*2<r){
v[k].erase(v[k].find(a));
}
else{
break;
}
}
if(v[k].size()==0){
v[k].insert(d);
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>r;
v.resize(n+1);
adj.resize(n);
dp.resize(n+1);
for(int i=1;i<n;i++){
int p;cin>>p;
adj[p].push_back(i);
}
dfs(0,0);
cout<<dp[0]+v[0].size()<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |