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: Time Limit Exceeded)

using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Math;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;

class P
{
    static void Main()
    {
        var nk = Console.ReadLine().Split().Select(int.Parse).ToList();
        List<int>[] neighbours = Enumerable.Repeat(0, nk[0]).Select(_ => new List<int>()).ToArray();
        List<Tuple<int, int>> edges = new List<Tuple<int, int>>();
        for (int i = 0; i < nk[0] - 1; i++)
        {
            var ab = Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToList();
            neighbours[ab[0]].Add(ab[1]);
            neighbours[ab[1]].Add(ab[0]);
            edges.Add(new Tuple<int, int>(ab[0], ab[1]));
        }

        Dictionary<int, int>[] indexes = neighbours.Select(x => x.Select((y, z) => new Tuple<int, int>(y, z)).ToDictionary(y => y.Item1, y => y.Item2)).ToArray();
        int[][][] datas = neighbours.Select(x => new int[x.Count][]).ToArray();

        Func<int, int, int[]> dfs = null;
        dfs = (x, from) =>
        {
            IEnumerable<int> enumerable = Enumerable.Repeat(0, 2000);
            for (int i = 0; i < datas[x].Length; i++)
            {
                var dest = neighbours[x][i];
                if (dest == from) continue;
                enumerable = enumerable.Zip(Enumerable.Repeat(0, 1).Concat(datas[x][i] != null ? datas[x][i] : (datas[x][i] = dfs(dest, x))), (a, b) => a + b);
            }
            var ret = enumerable.ToArray();
            ret[0] = 1;
            return ret;
        };
        int[][] dfsRes = new int[nk[0]][];
        for (int i = 0; i < nk[0]; i++) dfsRes[i] = dfs(i, -1);

        int res = int.MaxValue;
        if (nk[1] % 2 == 0)
        {
            res = Enumerable.Range(0, nk[0]).Min(x => dfsRes[x].Skip(nk[1] / 2 + 1).Sum());
        }
        else
        {
            res = edges.Min(x => datas[x.Item1][indexes[x.Item1][x.Item2]].Zip(datas[x.Item2][indexes[x.Item2][x.Item1]], (y, z) => y + z).Skip(nk[1] / 2 + 1).Sum());
        }
        Console.WriteLine(res);
    }
}