제출 #892905

#제출 시각아이디문제언어결과실행 시간메모리
892905LCJLYCat in a tree (BOI17_catinatree)C++14
100 / 100
84 ms31588 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl; 
#define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl;
typedef pair<int,int>pii;
typedef pair<pii,pii>pi2;

int n,k;
vector<int>adj[200005];
int dp[200005];
int d[200005];
int storage[2000005];

void dfs(int index, int par){
	int next=INT_MAX;
	int counter=0;
	vector<int>v;
	for(auto it:adj[index]){
		if(it==par) continue;
		d[it]=d[index]+1;
		dfs(it,index);
		next=min(next,storage[it]-d[index]);
		counter+=dp[it];
		v.push_back(storage[it]);
	}
	
	sort(v.begin(),v.end(),greater<>());
	while(v.size()>=2&&v[v.size()-1]+v[v.size()-2]-2*d[index]<k){
		v.pop_back();
		counter--;
	}
	
	if(next>=k){
		storage[index]=d[index];
		counter++;
	}
	else{
		storage[index]=v.back();
	}
	
	dp[index]=counter;
}

void solve(){	
	cin >> n >> k;
	
	int temp;
	for(int x=1;x<n;x++){
		cin >> temp;
		adj[x].push_back(temp);
		adj[temp].push_back(x);
	}
	
	dfs(1,-1);
	
	int best=0;
	for(int x=0;x<n;x++){
		best=max(best,dp[x]);
	}
	
	cout << best;
	
	
}

int32_t main(){										
	ios::sync_with_stdio(0);	
	cin.tie(0);
	//freopen("in.txt", "r", stdin);
	int t=1;
	//cin >> t;
	while(t--){
		solve();
	}	
}



		


		
		
	
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...