Language: C++

#include <cmath>
#include <map>
#include<fstream>
#include<algorithm>
#include <iostream>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>
#include <cassert>
#include <stack>
#include <ctime>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include <queue>
#include <vector>
#include <iterator>
#include <math.h>
#include<cstring>
#include <cmath>
#include <sstream>
#include<cstring>
#include<cstdlib>
#include <set>
#include <cstdio>
#include<map>
#define all(v)              ((v).begin()), ((v).end())
#define sz(v)               ((int)((v).size()))
#define REP(i, v)       for(int i=0;i<sz(v);i++)
#define dbg(a) cerr << #a << ": " << (a) << endl
#define ll long long
#define endl "\n"
#define getBit(num,idx) ((num >> idx) & 1)
#define setBit(num,idx) num|((1 << idx))
using namespace std;
typedef vector<vector<int> > vvi;
typedef vector<int> vi;
vector<int> leafs;
int chCnt[2000+5];
vector<vector<int> > tree(2000+5);
int parent[2000+5];
int P[2000+5][50];
int L[2000+5];
bool bad[2000+5];
pair<int,int> bfs(int u)
																																		{

	queue<pair<int,pair<int,int> > > Q; Q.push(make_pair(u,make_pair(0,-1)));
	int max_distance = 0; int t = u;
	while(!Q.empty())
	{
		pair<int,pair<int,int> > out = Q.front(); Q.pop();
		int idx = out.first; int dis = out.second.first; int p = out.second.second;

		if(bad[idx]) continue;
		if(max_distance <= dis){
			t = idx;
			max_distance  = dis;
		}
		REP(k,tree[idx])
		{
			int next = tree[idx][k];
			if(next==p || bad[next])continue;
			Q.push(make_pair(next,make_pair(dis+1,idx)));
		}


	}
	return make_pair(t,max_distance);
																																		}

pair<int,pair<int,int> > getDiameter(int idx)
																{

	int path = 0;


	pair<int,int> u = bfs(idx);
	pair<int,int> v = bfs(u.first);
	path = max(v.second,path);

	pair<int,pair<int,int> > PP = make_pair(path,make_pair(u.first,v.first));




	return PP;
																}

int solve(int i,int j)
{


	if(j == 0) return parent[i];



	if(P[i][j] != -1) return P[i][j];

	return P[i][j] = solve(solve(i,j-1),j-1);


}

int LCA_(int x,int y)
{

	if(L[x] > L[y])
	{
		int tmp = x;
		x = y;
		y = tmp;
	}

	int log = 25;


	// fix x to be on the same level as y
	for(int k = log ; k >= 0 ; k--)
	{

		int next = solve(y,k);
		// <= because we are going upwards
		if(L[x] <= L[next]) y = next;

	}
	// if it was a chain..then it's my ancestor
	if(x == y) return x;

	for(int k = log ; k >= 0; k--)
	{
		int nextX = solve(x,k); int nextY = solve(y,k);

		if(nextX != nextY)
		{
			// that ancestor that was found is not common
			x = nextX;
			y = nextY;
		}

	}


	return parent[x];

}

void dfs(int idx,int lvl)
{
	// performs dfs on the tree
	// store the level, and parent of each node
	// DFS must be called before any DP function..(obviously).
	L[idx] = lvl;
	chCnt[idx] = 0;
	REP(i,tree[idx])
	{
		int k = tree[idx][i];
		if(k == parent[idx]) continue;
		chCnt[idx]++;
		parent[k] = idx;
		dfs(k,lvl + 1);
	}

}

inline int dis(int i ,int j)
{
	return L[i] + L[j] - 2 * L[LCA_(i,j)];
}
int main()
{
	int N,K;
	scanf("%d %d",&N,&K);
	int u,v;

	for(int i = 0 ; i < N - 1; i++)
	{
		scanf("%d %d",&u,&v);
		--u; --v;
		tree[u].push_back(v);
		tree[v].push_back(u);
	}
	memset(P,-1,sizeof(P));
	parent[n - 1] = n - 1;
	dfs(n - 1,0);
	memset(bad,0,sizeof(bad));
	int DIA = 0;



	for(int i = 0 ; i < N ; i++)if(!chCnt[i]) leafs.push_back(i);

	int removed = 0;

	while(1)
	{
		pair<int,pair<int,int> > PP = getDiameter(n - 1);

		int firstNode = PP.second.first; int secondNode = PP.second.second;
		DIA = PP.first;



		if(DIA <= K) break;
		int choice = -1; int mini = 1 << 30;
		REP(k,leafs)
		{

			int next = leafs[k];
			if(bad[next]) continue;
			if(dis(next,firstNode) == DIA || dis(next,secondNode) == DIA)
			{
				if(parent[next] == (n - 1) || ( chCnt[parent[next]] < mini))
				{
					choice = next;
					if(parent[next] != 0) mini = chCnt[parent[next]];
					else mini = -1;
				}
			}
		}

		bad[choice] = 1;

		++removed;
		chCnt[parent[choice]]--;

		if(chCnt[parent[choice]] == 0)
		{

			leafs.push_back(parent[choice]);
		}


	}
	printf("%d",removed);

}


Compiler output:
a.cc: In function 'int main()':
a.cc:191:16: error: 'n' was not declared in this scope
  191 |         parent[n - 1] = n - 1;
      |                ^