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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;

// template {{{
/* vim: set foldmethod=marker: */

// constant
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;
const double PI = acos(-1);
#define EPS 1e-14

// typedef
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;

// input
template <class T> inline T in_(){ T x; cin >> x; return x; }

// short
#define pb push_back
#define mp make_pair
#define fst first
#define sec second

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n) - 1; i >= 0; i--)
#define REPit(type, i, obj) for (type::iterator i = (obj).begin(); i != (obj).end(); i++)
#define RREPit(type, i, obj) for (type::iterator i = (obj).rbegin(); i != (obj).rend(); i++)

// container util
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
#define sz(obj) obj.size()
#define sza(ary) (sizeof(ary) / sizeof(*ary))
#define sort(obj) sort(all(obj))
#define exist(obj, key) ((obj).find(key) != (obj).end())
#define cleAR(ary) memset((ary), 0, sizeof(ary))

// print util
#define print(x) cout << (x) << endl
#define printd(x, d) cout << fixed << setprecision(d) << (x) << endl
#define printv(v) REP(i, sz(v)) cout << v[i] << "\n "[i < sz(v) - 1]
#define printa(ary) REP(i, sza(ary)) cout << ary[i] << "\n "[i < sza(ary) - 1]
#define dump(x) cerr << #x << ": " << (x) << endl
#define debug(x) cerr << #x << ": " << (x) << " (" << __LINE__ << ")" << endl

//template }}}

vi edge[2048];
bool visited[2048][2048];
int d[2048][2048];
int n, k;

int remove_num(int a, int b)
{
    int rnum = 0;
    FOR(i, 1, n + 1){
        if (k / 2 < d[a][i] && k / 2 < d[b][i]) rnum++;
    }
    return rnum;
}

signed main()
{
    n = in_<int>();
    k = in_<int>();
    REP(i, n - 1){
        int a = in_<int>(), b = in_<int>();
        edge[a].push_back(b);
        edge[b].push_back(a);
    }

    FOR(i, 1, n + 1){
        queue<pii> que;
        for (que.push(pii(i, 0)); sz(que); ){
            pii p = que.front(); que.pop();
            if (visited[i][p.fst]) continue;
            visited[i][p.fst] = true;
            d[i][p.fst] = p.sec;
            for (int j : edge[p.fst]) que.push(pii(j, p.sec + 1));
        }
    }

    int mini = n;
    FOR(i, 1, n + 1){
        FOR(j, 1, n + 1){
            if (k % 2 < d[i][j]) continue;
            // printf(" (%d,%d):%d", i, j, remove_num(i, j));
            mini = min(mini, remove_num(i, j));
        }
        // puts("");
    }
    print(mini);

    return 0;
}