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

#![allow(unused_imports, unused_variables, dead_code, non_snake_case, unused_macros)]
use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;
fn getline() -> String {
    let mut res = String::new();
    std::io::stdin().read_line(&mut res).ok();
    res
}
macro_rules ! readl { ( $ t : ty ) => { { let s = getline ( ) ; s . trim ( ) . parse ::<$ t > ( ) . unwrap ( ) } } ; ( $ ( $ t : ty ) ,+ ) => { { let s = getline ( ) ; let mut iter = s . trim ( ) . split ( ' ' ) ; ( $ ( iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) , ) * ) } } ; }
macro_rules ! readlvec { ( $ t : ty ) => { { let s = getline ( ) ; let iter = s . trim ( ) . split ( ' ' ) ; iter . map ( | x | x . parse ( ) . unwrap ( ) ) . collect ::< Vec <$ t >> ( ) } } }
macro_rules ! mvec { ( $ v : expr , $ s : expr ) => { vec ! [ $ v ; $ s ] } ; ( $ v : expr , $ s : expr , $ ( $ t : expr ) ,* ) => { vec ! [ mvec ! ( $ v , $ ( $ t ) ,* ) ; $ s ] } ; }
macro_rules ! debug { ( $ x : expr ) => { println ! ( "{}: {:?}" , stringify ! ( $ x ) , $ x ) } }
fn printiter<'a, T>(v: &'a T)
where
    &'a T: std::iter::IntoIterator,
    <&'a T as std::iter::IntoIterator>::Item: std::fmt::Display,
{
    for (i, e) in v.into_iter().enumerate() {
        if i != 0 {
            print!(" ");
        }
        print!("{}", e);
    }
    println!("");
}
struct ContestPrinter {
    s: String,
}
impl ContestPrinter {
    fn new() -> ContestPrinter {
        ContestPrinter { s: String::new() }
    }
    fn print<T>(&mut self, x: T)
    where
        T: std::fmt::Display,
    {
        self.s.push_str(format!("{}", x).as_str());
    }
    fn println<T>(&mut self, x: T)
    where
        T: std::fmt::Display,
    {
        self.s.push_str(format!("{}\n", x).as_str());
    }
}
impl std::ops::Drop for ContestPrinter {
    fn drop(&mut self) {
        print!("{}", self.s);
    }
}
static MOD: i64 = 1e9 as i64 + 7;
fn is_max_i64(num: i64) -> bool {
    if num == i64::max_value() {
        true
    } else {
        false
    }
}

struct WarshallFloyd {
    pub cost: Vec<Vec<i64>>,
}
impl WarshallFloyd {
    fn calc(&mut self) -> i64 {
        let node_num = self.cost.len();
        assert!(node_num == self.cost[0].len(), "invalid cost");
        let mut diameter = 0;
        for k in 0..node_num {
            for i in 0..node_num {
                for j in 0..node_num {
                    if is_max_i64(self.cost[i][k]) | is_max_i64(self.cost[k][j]) {
                        continue;
                    }
                    let newcost = min(self.cost[i][j], self.cost[i][k] + self.cost[k][j]);
                    self.cost[i][j] = newcost;
                    diameter = max(diameter, newcost);
                }
            }
        }
        diameter
    }
}

fn main() {
    let mut pr = ContestPrinter::new();
    let (N, K) = readl!(usize, i64);
    let mut wf = WarshallFloyd{ cost: vec![vec![i64::max_value(); N]; N] };
    let mut ab = Vec::new();
    for i in 0..N-1 {
        let (a, b) = readl!(usize, usize);
        wf.cost[a-1][b-1] = 1;
        wf.cost[b-1][a-1] = 1;
        ab.push((a-1, b-1));
    }

    let d = wf.calc();
    let mut ans = i64::max_value();
    if d > K {
        if (K % 2) == 0 {
            let k2 = K / 2;
            for i in 0..N { // center node
                let mut cnt = 0; 
                for j in 0..N {
                    if i == j { continue; }
                    if wf.cost[i][j] > k2 { cnt += 1; }
                }
                ans = min(ans, cnt);
            }
        } else {
            let k12 = (K - 1) / 2;
            for i in 0..ab.len() {; // center edge
                let mut cnt = 0;
                let (a, b) = ab[i];
                for j in 0..N {
                    if (a == j) | (b == j) { continue; }
                    if min(wf.cost[a][j], wf.cost[b][j]) > k12 {
                        cnt += 1;
                    }
                }
                ans = min(ans, cnt);
            }
        }
    } else {
        ans = 0;
    }

    pr.println(ans);
}