이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |