# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
143011 | davitmarg | Cat in a tree (BOI17_catinatree) | C++17 | 7 ms | 5116 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <iomanip>
#include <stack>
#include <cassert>
#include <iterator>
#include <bitset>
#include <fstream>
#define mod 998244353ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(),v.end()
using namespace std;
int n,d,dp[2000][2000],upd[2000][2000],sz[200005],ans;
vector<int> g[200005];
void dfs(int v)
{
sz[v]=1;
for(int i=0;i<g[v].size();i++)
{
int to=g[v][i];
dfs(to);
}
dp[v][0]=1;
for(int i=0;i<g[v].size();i++)
{
int to=g[v][i];
for(int k=0;k<=d;k++)
{
upd[v][min(k+1,d-k-1)]=max(upd[v][min(k+1,d-k-1)],dp[to][k]+dp[v][d-1-k]);
upd[v][k+1]=max(dp[to][k],upd[v][k+1]);
}
sz[v]+=sz[to];
for(int k=d;k>=0;k--)
{
dp[v][k]=max(dp[v][k],upd[v][k]);
dp[v][k]=max(dp[v][k],dp[v][k+1]);
upd[v][k]=0;
}
}
// cout<<"! "<<v<<endl;
// for(int k=0;k<=d && k<=sz[v];k++)
// cout<<k<<" = "<<dp[v][k]<<"\n";
for(int k=0;k<=d && k<=sz[v];k++)
ans=max(ans,dp[v][k]);
}
int main()
{
cin>>n>>d;
for(int i=1;i<n;i++)
{
int p;
scanf("%d",&p);
g[p].PB(i);
}
dfs(0);
cout<<ans<<endl;
return 0;
}
/*
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |