Submission #1271785

#TimeUsernameProblemLanguageResultExecution timeMemory
1271785elotelo966Cat in a tree (BOI17_catinatree)C++20
51 / 100
16 ms18192 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long
#define OYY LLONG_MAX
#define faster ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define fi first
#define se second
#define FOR for(int i=1;i<=n;i++)
#define mid (start+end)/2
#define pb push_back
#define lim 1505

typedef long long lo;

int n,d;

vector<int> v[lim];

int dp[lim][lim];

inline void f(int node){
	dp[node][0]=1;
	for(auto go:v[node]){
		f(go);
		dp[node][0]+=dp[go][d-1];
	}
	
	for(int j=1;j<=d;j++){
		int tot=0,tot2=0,maxi=0;
		for(auto go:v[node]){
			tot+=dp[go][d-j-1];
			tot2+=dp[go][j-1];
			maxi=max(maxi,dp[go][j-1]);
		}
		//if(node==2)cout<<"k"<<j<<" "<<tot<<endl;
		for(auto go:v[node]){
			tot-=dp[go][d-j-1];
			tot+=dp[go][j-1];
			//if(node==2)cout<<node<<" "<<go<<" -> "<<tot<<" "<<dp[node][j]<<" "<<j<<endl;
			if(j<=d-j)dp[node][j]=max(dp[node][j],tot);
			tot-=dp[go][j-1];
			tot+=dp[go][d-j-1];
		}
		//if(node==2 && j==3)cout<<maxi<<" "<<dp[node][j]<<" "<<tot2<<endl;
		if(j>=d-j){
			dp[node][j]=max(dp[node][j],tot2);
			//if(node==1)cout<<node<<" "<<j<<" "<<d-j<<" "<<tot2<<endl;
		}
		if(j<=d-j){
			dp[node][j]=max(dp[node][j],maxi);
		}
	}
	
	for(int j=d;j>=0;j--){
		dp[node][j]=max(dp[node][j],dp[node][j+1]);
	}
}

int32_t main(){
	faster
	cin>>n>>d;
	d=min(d,n+2);
	
	FOR{
		if(i==1)continue;
		int x;cin>>x;
		x++;
		v[x].pb(i);
	}
	
	f(1);
	
	//~ FOR{
		//~ cout<<i<<" ";
		//~ for(int j=0;j<=d;j++){
			//~ cout<<dp[i][j]<<" ";
		//~ }
		//~ cout<<'\n';
	//~ }
	
	int cev=0;
	
	FOR{
		for(int j=0;j<=d;j++){
			cev=max(cev,dp[i][j]);
		}
	}
	
	cout<<cev<<'\n';
	
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...