Submission #937130

#TimeUsernameProblemLanguageResultExecution timeMemory
937130lucriCat in a tree (BOI17_catinatree)C++17
51 / 100
52 ms27052 KiB
#include <bits/stdc++.h> using namespace std; int pd[1510][1510],x,d,ans,n; vector<vector<int>>a; void calculeaza(int nod) { int sum[1510],vmax[1510]; for(int i=0;i<=d;++i) { sum[i]=0; vmax[i]=0; } for(auto x:a[nod]) { calculeaza(x); for(int i=0;i<=d;++i) { sum[i]+=pd[x][i]; if(i+2<=d) vmax[i]=max(vmax[i],pd[x][i]-pd[x][d-i-2]); } } pd[nod][d+1]=sum[d]; for(int i=d;i>=d/2+1;--i) { pd[nod][i]=sum[i-1]; pd[nod][i]=max(pd[nod][i],pd[nod][i+1]); } for(int i=d/2;i>=1;--i) { pd[nod][i]=vmax[i-1]+sum[d-i-1]; pd[nod][i]=max(pd[nod][i],pd[nod][i+1]); } pd[nod][0]=1+sum[d-1]; pd[nod][0]=max(pd[nod][0],pd[nod][1]); } int main() { cin>>n>>d; a.resize(n+5); for(int i=1;i<n;++i) { cin>>x; a[x].push_back(i); } calculeaza(0); for(int i=0;i<=d;++i) ans=max(ans,pd[0][i]); cout<<ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...