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, k);
			for (var i = 0; i < n - 1; i++) {
				input = sr.ReadArray(Int32.Parse);
				graph.Insert(input[0] - 1, input[1] - 1);
			}
			sw.WriteLine(graph.Max(0));
		}
	}

	class Graph
	{
		private readonly List<int>[] edges;
		private readonly int k;
		private int answer = 0;

		public Graph(int vertexCount, int k)
		{
			edges = new List<int>[vertexCount];
			this.k = k;
		}

		public List<int>[] Edges
		{
			get { return edges; }
		}

		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 Max(int start)
		{
			var states = new State[edges.Length];

			Dfs(0, states, 0);

			return answer;
		}

		private SortedSet<int> Dfs(int v, State[] states, int currLen)
		{
			states[v] = State.Discovered;
			var lens = new List<SortedSet<int>>();
			if (edges[v] != null) {
				for (var i = 0; i < edges[v].Count; i++) {
					if (states[edges[v][i]] == State.NotDiscovered) {
						var next = Dfs(edges[v][i], states, currLen + 1);
						if (next.Count > 0) {
							lens.Add(next);
						}
					}
				}
			}
			states[v] = State.Processed;
			if (lens.Count == 1) {
				if (lens[0].Max > k) {
					answer++;
					return new SortedSet<int>();
				}
				else {
					return new SortedSet<int> { lens[0].Max + 1 };
				}
			}
			if (lens.Count == 0) {
				return new SortedSet<int> { 1 };
			}
			var maxes = new int[lens.Count];
			for (var i = 0; i < lens.Count; i++) {
				maxes[i] = lens[i].Max;
			}
			Array.Sort(maxes, (x, y) => -x.CompareTo(y));
			var max = maxes[0];
			var nextMax = maxes[1];
			if (max + nextMax > k) {
				answer++;
				return new SortedSet<int>();
			}

			return new SortedSet<int> { max + 1 };
		}
	}

	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);
	}
}