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>
#include <assert.h>
using namespace std;
typedef int ll;
typedef long double ld;
#define PB push_back
#define MP make_pair
#define MOD 1000000007LL
#define endl "\n"
const ll UNDEF = -1;
const ll INF=1e9;
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, true : false; }
template<typename T> inline bool chkmin(T &a, T b) { return a > b ? a = b, true : false; }
const ll MAXN=2004;
vector<ll> g[MAXN];
ll dist[MAXN];
ll n;
bool valid[MAXN];
void bfs(ll init) {
	for (ll x=0;x<n;x++) dist[x]=INF;
	queue<ll> q;
	dist[init]=0;
	q.push(init);
	while(!q.empty()) {
		ll x=q.front();
		q.pop();
		for (auto &w:g[x]) {
			if (valid[w]&&dist[w]==INF) {
				dist[w]=dist[x]+1;
				q.push(w);
			}
		}
	}
}
ll findfar() {
	pair<ll,ll> ans=MP(-1,-1);
	for (ll x=0;x<n;x++) {
		if(dist[x]!=INF)chkmax(ans,MP(dist[x],x));
	}
	return ans.second;
}
int main() {
	ios_base::sync_with_stdio(false);
	ll best=INF;
	ll k; scanf("%d %d",&n,&k);
	for (ll i=0;i<n-1;i++) {
		ll a,b; scanf("%d %d",&a,&b); a--;b--;
		g[a].PB(b); g[b].PB(a);
	}
	for (ll tt=0;tt<80;tt++) {
	for (ll x=0;x<n;x++) valid[x]=true;
	for (ll i=0;;i++) {
		ll val=-1; for (ll x=0;x<n;x++) {
			if(valid) {val=x; break;}
		}
		bfs(val);
		ll u=findfar();
		bfs(u);
		ll v=findfar();
		ll diam=dist[v];
		if (diam<=k) {
			//printf("u:%d v:%d dist:%d\n",u,v,dist[v]);
			chkmin(best,i);
			break;
		}
		ll uc=0,vc=0;
		for (ll x=0;x<n;x++) {
			if (dist[x]==diam) uc++;
		}
		bfs(v);
		for (ll x=0;x<n;x++) {
			if (dist[x]==diam) vc++;
		}
		ll rm=(uc>vc)?u:v;
		if (uc==vc) {
			rm=(rand()%2)?u:v;
		}
		//printf("rm:%d\n",rm);
		valid[rm]=false;
	}
	}
	printf("%d\n",best);
}