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 Rust (1.15.1) (judge verdict: Wrong Answer)

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

fn make_tree(
    cur_idx: usize,
    parent_idx: usize,
    nodes: &Vec<Vec<usize>>,
    tree: &mut Vec<Vec<usize>>,
) {
    for child_idx in nodes[cur_idx].iter() {
        if *child_idx == parent_idx {
            continue;
        }
        tree[cur_idx].push(*child_idx);
        make_tree(*child_idx, cur_idx, nodes, tree);
    }
}

fn dfs(
    cur_idx: usize,
    cur_deep: usize,
    max_depth: usize,
    tree: &Vec<Vec<usize>>,
    nodes: &mut Vec<usize>,
) {
    if cur_idx > max_depth {
        return;
    }
    nodes.push(cur_idx);
    for &child_idx in tree[cur_idx].iter() {
        dfs(child_idx, cur_deep + 1, max_depth, tree, nodes);
    }
}

fn main() {
    let v: Vec<usize> = read_vec();
    let (n, k) = (v[0], v[1]);
    let mut nodes = vec![Vec::new(); n];
    let mut edges = Vec::new();
    for i in 0..n - 1 {
        let v: Vec<usize> = read_vec();
        let (a, b) = (v[0] - 1, v[1] - 1);
        nodes[a].push(b);
        nodes[b].push(a);
        edges.push((a, b));
    }

    let mut ans = 0;
    if k % 2 == 0 {
        for root in 0..n {
            let mut tree: Vec<Vec<usize>> = vec![Vec::new(); n];
            make_tree(root, n, &nodes, &mut tree);
            let mut nodes = Vec::new();
            dfs(root, 0, k, &tree, &mut nodes);
            ans = max(ans, nodes.len());
            // println!("{0}, {1}", root, nodes.len());
        }
    } else {
        for (node1, node2) in edges {
            let mut tree: Vec<Vec<usize>> = vec![Vec::new(); n];
            make_tree(node1, node2, &nodes, &mut tree);
            let mut nodes1 = Vec::new();
            dfs(node1, 0, k - 1, &tree, &mut nodes1);

            let mut tree: Vec<Vec<usize>> = vec![Vec::new(); n];
            make_tree(node2, node1, &nodes, &mut tree);
            let mut nodes2 = Vec::new();
            dfs(node1, 0, k - 1, &tree, &mut nodes2);
            ans = max(ans, nodes1.len() + nodes2.len());
        }
    }
    println!("{}", n - 1 - ans);
}