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: Wrong Answer)

#include <iostream>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <algorithm>
#include <functional>
#include <limits.h>
#include <bitset>

#include <tuple>
#include <unordered_map>

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
const int MAX_N = 2010;
int deg[MAX_N];
vector<int> edges[MAX_N];
set<int> taboo_nodes;
int N, K;
int root;
int dist[MAX_N];
void dfs(int pos) {
  int res = 0;
  for (const int next : edges[pos]) {
    if (taboo_nodes.count(next) > 0)
      continue;
    if (dist[next] != INF)
      continue;
    dist[next] = dist[pos] + 1;
    dfs(next);
  }
}
void fill_dist_inf() {
  for (int i = 0; i < N; i++) {
    dist[i] = INF;
  }
}
bool visited[MAX_N];
bool get_path(int pos, int tar, vector<int> &path) {
  path.push_back(pos);
  visited[pos] = true;
  if (pos == tar) {
    return true;
  }
  for (const int next : edges[pos]) {
    if (taboo_nodes.count(next) > 0)
      continue;
    if (visited[next])
      continue;
    if (get_path(next, tar, path)) {
      return true;
    }
  }
  path.pop_back();
  return false;
}
int main() {
  root = 0;
  scanf("%d %d\n", &N, &K);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    a--, b--;
    deg[a]++;
    deg[b]++;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }

  auto diameter = [&]() {
    for (int i = 0; i < N; i++) {
      if (taboo_nodes.count(i) == 0) {
        root = i;
        break;
      }
    }
    fill_dist_inf();
    dist[root] = 0;
    dfs(root);
    int left = root;
    for (int i = 0; i < N; i++) {
      if (taboo_nodes.count(i) > 0)
        continue;
      if (dist[left] < dist[i]) {
        left = i;
      }
    }
    fill_dist_inf();
    dist[left] = 0;
    dfs(left);
    int right = root;
    for (int i = 0; i < N; i++) {
      if (taboo_nodes.count(i) > 0)
        continue;
      if (dist[right] < dist[i]) {
        right = i;
      }
    }
    return mt(left, right, dist[right]);
  };
  int cnt = 0;
  while (true) {
    auto val = diameter();
    int left, right, len;
    tie(left, right, len) = val;
    if (len <= K)
      break;
    memset(visited, false, sizeof(visited));

    vector<int> path;
    get_path(left, right, path);
    set<int> path_nodes;
    for (const auto &val : path) {
      path_nodes.insert(val);
      // cerr << val << " ";
    }
    // cerr << endl;
    int par_left, par_right;
    par_left = path[1];
    par_right = path[path.size() - 2];
    // cerr << par_left << " " << par_right << endl;
    // cerr << deg[par_left] << " " << deg[par_right] << endl;
    if (deg[par_left] < deg[par_right]) {
      for (const int &node : edges[par_left]) {
        if (taboo_nodes.count(node) == 0 &&
            (path_nodes.count(node) == 0 || node == left)) {
          // cerr << "node = " << node << endl;
          taboo_nodes.insert(node);
          deg[par_left]--;
          deg[node]--;
        }
      }
    } else {
      for (const int &node : edges[par_right]) {

        if (taboo_nodes.count(node) == 0 &&
            (path_nodes.count(node) == 0 || node == right)) {
          // cerr << "node = " << node << endl;
          taboo_nodes.insert(node);
          deg[par_right]--;
          deg[node]--;
        }
      }
    }
    // cnt++;
    // if (cnt >= 3)
    //   break;
  }
  // cerr << left << " " << right << " " << dist[right] << endl;
  cout << taboo_nodes.size() << endl;
  return 0;
}