Language: C++

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v,n) reverse(v,v+n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define m0(x) memset(x,0,sizeof(x))
#define print(x) cout<<x<<endl;
#define pe(x) cout<<x<<" ";
#define lb(v,n) lower_bound(v.begin(), v.end(), n);
#define ub(v,n) upper_bound(v.begin(), v.end(), n);
#define int long long
#define all(x) (x).begin(), (x).end()

using namespace std;

const int MAX = 1010;
const int MOD = (ll)1000000000 + 7;
const ll INF = 1e17;
const double pi = acos(-1);
const double EPS = 1e-10;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
typedef pair<int, int>P;

int dx[4] = { 1,0,-1,0 };//D,R,U,L
int dy[4] = { 0,1,0,-1 };

const int MAX_N = 1 << 17;
const int MAX_V = 2020;
int n, dat[2 * MAX_N - 1];
int id[MAX_V];

void init2(int n_) {
	n = 1;
	while (n < n_)n *= 2;
	for (int i = 0; i < 2 * n - 1; i++)dat[i] = INT_MAX;
}

void update(int k, int a) {
	k += n + 1;
	dat[k] = a;
	while (k > 0) {
		k = (k - 1) / 2;
		dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
	}
}

void rmq_init(int depth[], int n) {
	init2(n);
	REP(i, n) {
		update(i, depth[i]);
	}
}

int query(int a, int b, int k, int l, int r) {
	if (r <= a || b <= l)return INT_MAX;
	if (a <= l && r <= b)return dat[k];
	else {
		int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
		int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
		return min(vl, vr);
	}
}





vector<int>G[MAX_V];
int root;

int vs[MAX_V * 2 - 1];
int depth[MAX_V * 2 - 1];

int V;

void dfs(int v, int p, int d, int &k) {
	id[v] = k;
	vs[k] = v;
	depth[k++] = d;
	for (int i = 0; i < G[v].size(); i++) {
		if (G[v][i] != p) {
			dfs(G[v][i], v, d + 1, k);
			vs[k] = v;
			depth[k++] = d; 
		}
	}
}
void init(int V) {
	int k = 0;
	dfs(root, -1, 0, k);
	rmq_init(depth, V * 2 - 1);
}

int lca(int u, int v) {
	return vs[query(min(id[u], id[v]), max(id[u], id[v]) + 1, (ll)0, (ll)0, 2*V-1)];
}

int dist(int a, int b) {
	int par = lca(a, b);
	return depth[id[a]] + depth[id[b]] - 2 * depth[id[par]];
}

int A[2020], B[2020];
signed main() {
	int N, K; cin >> N >> K;
	V = N;
	root = 0;
	//G[0].pb(1); G[1].pb(0);
	REP(i, N-1) {
		int a, b; cin >> a >> b;
		a--, b--;
		A[i] = a, B[i] = b;
		G[a].pb(b);
		G[b].pb(a);
	}
	init(N);
	int ans = INF;
	if (K % 2 == 0) {
		REP(i, N) {
			pe(i);
			int res = 0;
			REP(j, N) {
				if (j == i)continue;
				if (dist(i, j) > K / 2)res++;
			}
			ans = min(ans, res);
			print(res);
		}
	}
	else {
		REP(i, N - 1) {
			int res = 0;
			pe(i);
			int l = A[i], r = B[i];
			REP(j, N) {
				if (j == l || j == r)continue;
				int d = min(dist(l, j), dist(r, j));
				if (d > (K - 1) / 2)res++;
			}
			print(res);
			ans = min(ans, res);
		}
	}
	print(ans);
	int x, y; cin >> x >> y;
	x--, y--;
	print(lca(x, y));
	print(dist(x, y));
}

Compiler output:
a.cc: In function 'void init2(long long int)':
a.cc:55:53: error: 'INT_MAX' was not declared in this scope
   55 |         for (int i = 0; i < 2 * n - 1; i++)dat[i] = INT_MAX;
      |                                                     ^~~~~~~
a.cc:15:1: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
   14 | #include<bitset>
  +++ |+#include <climits>
   15 | 
a.cc: In function 'long long int query(long long int, long long int, long long int, long long int, long long int)':
a.cc:75:37: error: 'INT_MAX' was not declared in this scope
   75 |         if (r <= a || b <= l)return INT_MAX;
      |                                     ^~~~~~~
a.cc:75:37: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'