Problem:
Score : 600 points

Problem Statement

Given an undirected tree, let the distance between vertices u and v be the
number of edges on the simple path from u to v.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree good if and only if its diameter is at most K.

You are given an undirected tree with N vertices numbered 1 through N.
For each i (1≦i≦N-1), there is an edge connecting vertices A_i and B_i.

You want to remove zero or more vertices from the tree, so that the resulting tree is good.
When a vertex is removed, all incident edges will also be removed.
The resulting graph must be connected.

Find the minimum number of vertices that you need to remove in order to produce a good tree.

Constraints

2≦N≦2000

1≦K≦N-1

1≦A_i≦N, 1≦B_i≦N

The graph defined by A_i and B_i is a tree.

Input

The input is given from Standard Input in the following format:

N K
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}

Output

Print the minimum number of vertices that you need to remove in order to produce a good tree.

Sample Input 1

6 2
1 2
3 2
4 2
1 6
5 6

Sample Output 1

2

The tree is shown below. Removing vertices 5 and 6 will result in a good tree with the diameter of 2.

Sample Input 2

6 5
1 2
3 2
4 2
1 6
5 6

Sample Output 2

0

Since the given tree is already good, you do not need to remove any vertex.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cout;
using std::cerr;
using std::endl;
using std::cin;

template<typename T>
std::vector<T> make_v(size_t a){return std::vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

int main() {
	int n, k; scanf("%d%d", &n, &k);
	std::vector<int> a(n - 1), b(n - 1);
	std::vector<std::vector<int>> g(n);
	for(int i = 0; i < n - 1; i++) {
		scanf("%d%d", &a[i], &b[i]);
		a[i]--; b[i]--;

		g[a[i]].push_back(i);
		g[b[i]].push_back(i);
	}

	auto calc_dist = [&](int s) {
		std::queue<int> qu({s});
		std::vector<int> dist(n, 1 << 30); dist[s] = 0;
		while(!qu.empty()) {
			auto v = qu.front(); qu.pop();

			for(auto id: g[v]) {
				int to = a[id] ^ b[id] ^ v;
				if(dist[to] <= dist[v] + 1) continue;
				dist[to] = dist[v] + 1;
				qu.push(to);
			}
		}
		return dist;
	};
	auto calc_cnt = [&](int s) {
		std::vector<int> cnt(n);
		auto dfs = [&](auto&& dfs, int v, int p) -> void {
			cnt[v] = 1;
			for(int id: g[v]) {
				int to = a[id] ^ b[id] ^ v;
				if(to == p) continue;
				dfs(dfs, to, v);
				cnt[v] += cnt[to];
			}
		};
		dfs(dfs, s, -1);

		return cnt;
	};

	int ans = 1 << 30;
	for(int i = 0; i < n; i++) {
		auto dist = calc_dist(i);
		auto size = calc_cnt(i);

		int sum = 0;
		for(int id: g[i]) {
			int to = a[id] ^ b[id] ^ i;
			sum += size[to];
		}
		for(int id: g[i]) {
			int to = a[id] ^ b[id] ^ i;

			auto dfs = [&](auto&& dfs, int v, int par, int d) -> int {
				int ret = d > k;
				for(int id: g[v]) {
					int to = a[id] ^ b[id] ^ v;
					if(to == par) continue;
					ret += dfs(dfs, to, v, d + 1);
				}
				return ret;
			};
			int ret = dfs(dfs, to, i, 1);

			ans = std::min(ans, (sum - size[to]) + ret);
		}
	}

	printf("%d\n", ans);
	return 0;
}