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;

#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(int i = (a); i >= (b); --i)
#define RI(i,n) FOR(i,1,(n))
#define REP(i,n) FOR(i,0,(n)-1)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define sz(w) (int) w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<pii, int> para;
const int inf = 1e9 + 7;
const int maxN = 1e6 + 5;

int m, a, b, ans, z, k, n;
vector<vector<ll>> A(105);
bool used[maxN];
int arr[maxN], arr1[maxN], odl[maxN], odl2[maxN], parent[maxN];
vi graph[maxN];
queue<int> Q;

void BFSParent(int start) {
	parent[start] = -1;
	Q.push(start);
	while (!Q.empty()) {
		int u = Q.front();
		Q.pop();
		REP(i, sz(graph[u])) {
			if (parent[graph[u][i]] == 0) {
				parent[graph[u][i]] = u;
				Q.push(graph[u][i]);
			}
		}
	}
}

para srednica() {

	int v;
	RI(i, n) {
		if (!used[i]) {
			v = i;
			break;
		}
	}
	odl[v] = 0;
	Q.push(v);
	int vert = 1, maxim = 0;
	while (!Q.empty()) {
		int u = Q.front();
		Q.pop();
		REP(i, sz(graph[u])) {
			if (odl[graph[u][i]] == -inf && !used[graph[u][i]]) {
				odl[graph[u][i]] = odl[u] + 1;
				Q.push(graph[u][i]);
				if (maxim < odl[u] + 1) {
					maxim = odl[u] + 1;
					vert = graph[u][i];
				}
			}
		}
	}
	//printf("%d %d\n",vert,maxim);
	maxim = 0;
	int z = vert;
	Q.push(vert);
	odl2[vert] = 0;
	while (!Q.empty()) {
		int u = Q.front();
		Q.pop();
		REP(i, sz(graph[u])) {
			//printf("Kr : %d %d %d %d\n",u, graph[u][i], odl2[u], odl2[graph[u][i]]);
			if (odl2[graph[u][i]] == -inf && !used[graph[u][i]]) {
				//printf("Kr : %d %d\n",u, graph[u][i]);
				odl2[graph[u][i]] = odl2[u] + 1;
				Q.push(graph[u][i]);
				if (maxim < odl2[u] + 1) {
					maxim = odl2[u] + 1;
					vert = graph[u][i];
				}
			}
		}
	}
	
	return para(pii(z, vert), odl2[vert]);
}

int main() {
	scanf("%d%d",&n,&k);
	REP(i, n - 1) {
		scanf("%d%d",&a,&b);
		graph[a].pb(b);
		graph[b].pb(a);
		odl[i + 1] = odl2[i + 1] = -inf;
	}
	odl[n] = odl2[n] = -inf;
	BFSParent(1);
	para x = srednica();
	//printf("%d %d %d\n",x.st.st, x.st.nd, x.second);
	while (x.nd > k) {
		int size1 = 0, size2 = 0;
		REP(i, sz(graph[parent[x.st.st]])) {
			size1 += (!used[graph[parent[x.st.st]][i]]);
		}
		REP(i, sz(graph[parent[x.st.nd]])) {
			size2 += (!used[graph[parent[x.st.nd]][i]]);
		}
		//printf("Rozm %d %d\n",size1, size2);
		if (size1 > size2) {
			swap(x.st.st, x.st.nd);
			//printf("zamieniam");
		}
		used[x.st.st] = true;
		ans++;
		RI(i, n) {
			odl[i] = odl2[i] = -inf;
		}
		x = srednica();
		//printf("%d %d %d\n",x.st.st, x.first.second, x.second);
	}
	printf("%d\n",ans);
	return 0;
}