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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.util.*;

public class Main {
    private static final int INF = 114514;

    private class Road {
        int src, dst, cost;

        public Road(int src, int dst, int cost) {
            this.src = src;
            this.dst = dst;
            this.cost = cost;
        }
    }

    private class Node {
        int cost = INF;
        boolean fix = false;
        List<Road> child = new ArrayList<>();
    }

    public void main(Scanner sc) {
        int n = sc.nextInt();
        int k = sc.nextInt();

        Node nodes[] = new Node[n];
        for (int i = 0; i < n; i++) {
            nodes[i] = new Node();
        }
        for (int i = 0; i < n - 1; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            nodes[a].child.add(new Road(a, b, 1));
            nodes[b].child.add(new Road(b, a, 1));
        }

        int cost[][] = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                nodes[j].cost = INF;
                nodes[j].fix = false;
            }
            cost[i] = dij(i, n, nodes);
        }

        int ans = 0;
        if (k % 2 == 0) {
            for (int i = 0; i < n; i++) {
                int cnt = 0;
                for (int j = 0; j < n; j++) {
                    if (cost[i][j] <= k / 2) {
                        cnt++;
                    }
                }
                ans = Math.max(ans, cnt);
            }
        } else {
            BitSet bset1 = new BitSet();
            BitSet bset2 = new BitSet();

            for (int i = 0; i < n; i++) {
                bset1 = new BitSet();
                for (int j = 0; j < n; j++) {
                    if (cost[i][j] <= k / 2) {
                        bset1.set(j);
                    }
                }

                for (Road road : nodes[i].child) {
                    bset2 = new BitSet();
                    int dst = road.dst;
                    for (int j = 0; j < n; j++) {
                        if (cost[dst][j] <= k / 2) {
                            bset2.set(j);
                        }
                    }

                    bset2.or(bset1);
                    ans = Math.max(ans, bset2.cardinality());
                }
            }
        }
        System.out.println(n - ans);
    }

    private int[] dij(int start, int n, Node nodes[]) {
        nodes[start].cost = 0;
        Queue<Node> queue = new PriorityQueue<>((o1, o2) -> o1.cost - o2.cost);
        queue.add(nodes[start]);

        while (!queue.isEmpty()) {
            Node node = queue.poll();
            node.fix = true;
            for (Road road : node.child) {
                if (!nodes[road.dst].fix) {
                    nodes[road.dst].cost = Math.min(nodes[road.dst].cost, node.cost + road.cost);
                    queue.add(nodes[road.dst]);
                }
            }
        }

        int costs[] = new int[n];
        for (int i = 0; i < n; i++) {
            costs[i] = nodes[i].cost;
        }
        return costs;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        new Main().main(sc);
        sc.close();
    }
}