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<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
namespace March
{
	inline void check_min(int a,int &b){if(a<b)b=a;}
	inline void check_max(int a,int &b){if(a>b)b=a;}
	const int N=2050,M=N*2,INF=2147483647;
	std::vector<int> S1,S2;
	int begin[N],next[M],to[M];
	bool ded[N];
	int n,m,e,ans;
	void add(int x,int y,bool k=1)
	{
		to[++e]=y;
		next[e]=begin[x];
		begin[x]=e;
		if(k)add(y,x,0);
	}
	int make_it_shorter()
	{
		static int dl[N],dis[N];
		static bool vis[N];
		memset(vis,0,sizeof(vis));
		int u=0,d=0,tmp;

		for(tmp=1;ded[tmp];tmp++);
		vis[dl[++u]=tmp]=1;
		dis[tmp]=0;

		for(int p,q;u>d;)
		{
			p=dl[++d];
			for(int i=begin[p];i;i=next[i])
				if(!vis[q=to[i]] && !ded[q])vis[dl[++u]=q]=1,dis[q]=dis[p]+1;
		}
		S1.clear();
		for(int i=u;dis[dl[i]]==dis[dl[u]];i--)
			S1.push_back(dl[i]);

		memset(vis,0,sizeof(vis));
		u=0,d=0;
		vis[dl[++u]=S1[0]]=1;
		dis[S1[0]]=0;
		for(int p,q;u>d;)
		{
			p=dl[++d];
			for(int i=begin[p];i;i=next[i])
				if(!vis[q=to[i]] && !ded[q])vis[dl[++u]=q]=1,dis[q]=dis[p]+1;
		}
		S2.clear();
		for(int i=u;dis[dl[i]]==dis[dl[u]];i--)
			S2.push_back(dl[i]);

		if(dis[S2[0]]<=m)return 0;

		int ret;
		std::vector<int>::iterator t;
		if(S1.size()<S2.size())ret=S1.size(),t=S1.begin();
		else ret=S2.size(),t=S2.begin();
		for(int i=0;i<ret;i++)
			ded[t[i]]=1;
		return ret;
	}
	void initialize()
	{
		scanf("%d%d",&n,&m);
		for(int i=1,u,v;i<n;i++)
			scanf("%d%d",&u,&v),add(u,v);
	}
	void solve()
	{
		initialize();
		ans=0;
		for(int x;(x=make_it_shorter())!=0;)
			ans+=x;
		printf("%d\n",ans);
	}
}
int main()
{
	March::solve();
	return 0;
}