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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

namespace CF317
{
	internal class Program
	{
		private static void Main(string[] args)
		{
			var sr = new InputReader(Console.In);
			//var sr = new InputReader(new StreamReader("input.txt"));
			var task = new Task();
			using (var sw = Console.Out)
			//using (var sw = new StreamWriter("output.txt"))
			{
				task.Solve(sr, sw);
				//Console.ReadKey();
			}
		}
	}

	internal class Task
	{
		public void Solve(InputReader sr, TextWriter sw)
		{
			var input = sr.ReadArray(Int32.Parse);
			var n = input[0];
			var k = input[1];
			var graph = new Graph(n);
			for (var i = 0; i < n - 1; i++) {
				input = sr.ReadArray(Int32.Parse);
				graph.Insert(input[0] - 1, input[1] - 1);
			}
			sw.WriteLine(graph.Min(k));
		}
	}

	class Graph
	{
		private readonly List<int>[] edges;

		public Graph(int n)
		{
			edges = new List<int>[n];
		}

		public void Insert(int x, int y)
		{
			if (edges[x] == null) {
				edges[x] = new List<int>();
			}
			edges[x].Add(y);
			if (edges[y] == null) {
				edges[y] = new List<int>();
			}
			edges[y].Add(x);
		}

		public int Min(int k)
		{
			var min = Int32.MaxValue;
			for (var i = 0; i < edges.Length; i++) {
				var distances = new int[edges.Length];
				Bfs(i, distances);
				var curr = distances.Count(item => item > k / 2);
				min = Math.Min(min, curr);
			}

			return min;
		}

		private void Bfs(int s, int[] distances)
		{
			var queue = new Queue<Tuple<int, int>>();
			queue.Enqueue(new Tuple<int, int>(s, 0));
			var states = new State[edges.Length];
			states[s] = State.Discovered;
			while (queue.Count > 0) {
				var next = queue.Dequeue();
				distances[next.Item1] = next.Item2;
				if (edges[next.Item1] != null) {
					for (var i = 0; i < edges[next.Item1].Count; i++) {
						if (states[edges[next.Item1][i]] == State.NotDiscovered) {
							states[edges[next.Item1][i]] = State.Discovered;
							queue.Enqueue(new Tuple<int, int>(edges[next.Item1][i], next.Item2 + 1));
						}
					}
				}
				states[next.Item1] = State.Processed;
			}
		}
	}

	enum State
	{
		NotDiscovered,
		Discovered,
		Processed
	}
}

internal class InputReader : IDisposable
{
	private bool isDispose;
	private readonly TextReader sr;

	public InputReader(TextReader stream)
	{
		sr = stream;
	}

	public void Dispose()
	{
		Dispose(true);
		GC.SuppressFinalize(this);
	}

	public string NextString()
	{
		var result = sr.ReadLine();
		return result;
	}

	public int NextInt32()
	{
		return Int32.Parse(NextString());
	}

	public long NextInt64()
	{
		return Int64.Parse(NextString());
	}

	public string[] NextSplitStrings()
	{
		return NextString()
				.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
	}

	public T[] ReadArray<T>(Func<string, CultureInfo, T> func)
	{
		return NextSplitStrings()
				.Select(s => func(s, CultureInfo.InvariantCulture))
				.ToArray();
	}

	public T[] ReadArrayFromString<T>(Func<string, CultureInfo, T> func, string str)
	{
		return
				str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
						.Select(s => func(s, CultureInfo.InvariantCulture))
						.ToArray();
	}

	protected void Dispose(bool dispose)
	{
		if (!isDispose)
		{
			if (dispose)
				sr.Close();
			isDispose = true;
		}
	}

	~InputReader()
	{
		Dispose(false);
	}
}