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>
#define pb push_back
#define ll long long
#define ff first
#define ss second
#define N 200001
using namespace std;
vector<int>child[N];
int n,d;
struct res{
deque<int>dp;
int size(){
return dp.size();
}
};
void attach(res &root,res &children){
if(root.size()<children.size()) swap(root,children);
res combined;
combined.dp=children.dp;
for(int i=0;i<children.size();i++){
int other=max(d-i,i);
if(other<children.size()){
combined.dp[i]=max(combined.dp[i],root.dp[i]+children.dp[other]);
}
if(other<root.size()){
combined.dp[i]=max(combined.dp[i],root.dp[other]+children.dp[i]);
}
}
int maximum=0;
for(int i=children.size();i>=1;i--){
maximum=max(maximum,combined.dp[i-1]);
root.dp[i-1]=max(maximum,root.dp[i-1]);
}
}
res dfs(int node){
res ans;
ans.dp.push_back(1);
for(int children:child[node]){
res branch=dfs(children);
branch.dp.push_front(branch.dp.front());
attach(ans,branch);
}
return ans;
}
int main(){
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> d;
for(int i=1;i<n;i++){
int k;
cin >> k;
child[k].pb(i);
}
cout << dfs(0).dp.front();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |