Language: C++

#include <cstdio>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <vector>
#include <algorithm>
#include <set>
#include <deque>
#include <utility>
#include <chrono>
#include <sstream>
#include <iomanip>
#include <cassert>

#define INF 1 << 30
#define MOD 1000000007;
#define PI 3.14159265358979
#define rep(i, n) for (int (i) = 0; (i) < (int)(n); (i)++)
#define reu(i, l, r) for (int (i) = (int)(l); (i) < (int)(r); (i)++)
#define D(x) cout << x << endl
#define d(x) cout << x
#define all(x) (x).begin(), (x).end()
#define pub(x) push_back(x)
#define pob() pop_back()
#define puf(x) push_front(x)
#define pof() pop_front()
#define mp(x, y) make_pair((x), (y))
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
template<typename T, typename U> inline void amin(T &x, U y) { if (y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if (x < y) x = y; }

static const int dx[] = {0, 0, 1, -1};
static const int dy[] = {-1, 1, 0, 0};

int n, k;
vector<vi> e(2020);

int dfs(int x, int p, int d) {
        int ans = 1;
        if (d < k / 2) {
                for (int y : e[x]) {
                        if (y == p) {
                                continue;
                        }
                        ans += dfs(y, x, d + 1);
                }
        }
        return ans;
}

vector<int> Centroid(int root, const vector<vector<int>> &g, const vector<bool> &dead) {
        static vector<int> sz(g.size());
        int alive_cnt = 0;
        function<void (int, int)> count_alive = [&](int u, int prev) {
                alive_cnt ++;
                for (auto v : g[u]) if (v != prev && !dead[v]) {
                        count_alive(v, u);
                }
        };
        count_alive(root, -1);
        vector<int> centroid;
        function<void (int, int)> dfs = [&](int u, int prev) {
                sz[u] = 1;
                bool is_centroid = true;
                for (auto v : g[u]) if (v != prev && !dead[v]) {
                        dfs(v, u);
                        sz[u] += sz[v];
                        if (sz[v] > alive_cnt / 2) is_centroid = false;
                }
                if (alive_cnt - sz[u] > alive_cnt / 2) is_centroid = false;
                if (is_centroid) centroid.push_back(u);
        };
        dfs(root, -1);
        return centroid;
}

int OneCentroid(int root, const vector<vector<int>> &g, const vector<bool> &dead) {
        static vector<int> sz(g.size()); //caution
        function<void (int, int)> get_sz = [&](int u, int prev) {
                sz[u] = 1;
                for (auto v : g[u]) if (v != prev && !dead[v]) {
                        get_sz(v, u);
                        sz[u] += sz[v];
                }
        };
        get_sz(root, -1);
        int n = sz[root];
        function<int (int, int)> dfs = [&](int u, int prev) {
                for (auto v : g[u]) if (v != prev && !dead[v]) {
                        if (sz[v] > n / 2) {
                                return dfs(v, u);
                        }
                }
                return u;
        };
        return dfs(root, -1);
}

int main() {
        cin >> n >> k;
        vector<vector<int>> g(n);
        rep(i, n - 1) {
                int a, b;
                cin >> a >> b, -- a, -- b;
                e[a].pub(b);
                e[b].pub(a);
                g[a].push_back(b);
                g[b].push_back(a);
        }
        vector<bool> dead(n);
        vector<int> centroids = Centroid(0, g, dead);
        int c = OneCentroid(0, g, dead);
        assert(c == centroids[0] || c == centroids[1]);

        int ans = 0;
        if (k & 1) {
                rep(i, n) {
                        for (int j : e[i]) {
                                amax(ans, dfs(i, j, 0) + dfs(j, i, 0));
                        }
                }
        } else {
                rep(i, n) {
                        amax(ans, dfs(i, -1, 0)); 
                }
        }
        D(n - ans);
        return 0;
}


Compiler output:
a.cc: In function 'std::vector<int> Centroid(int, const std::vector<std::vector<int> >&, const std::vector<bool>&)':
a.cc:74:9: error: 'function' was not declared in this scope
   74 |         function<void (int, int)> count_alive = [&](int u, int prev) {
      |         ^~~~~~~~
a.cc:20:1: note: 'std::function' is defined in header '<functional>'; this is probably fixable by adding '#include <functional>'
   19 | #include <cassert>
  +++ |+#include <functional>
   20 | 
a.cc:74:32: error: expression list treated as compound expression in functional cast [-fpermissive]
   74 |         function<void (int, int)> count_alive = [&](int u, int prev) {
      |                                ^
a.cc:74:18: error: expected primary-expression before 'void'
   74 |         function<void (int, int)> count_alive = [&](int u, int prev) {
      |                  ^~~~
a.cc:80:9: error: 'count_alive' was not declared in this scope
   80 |         count_alive(root, -1);
      |         ^~~~~~~~~~~
a.cc:82:32: error: expression list treated as compound expression in functional cast [-fpermissive]
   82 |         function<void (int, int)> dfs = [&](int u, int prev) {
      |                                ^
a.cc:82:18: error: expected primary-expression before 'void'
   82 |         function<void (int, int)> dfs = [&](int u, int prev) {
      |                  ^~~~
a.cc:93:12: error: too few arguments to function 'int dfs(int, int, int)'
   93 |         dfs(root, -1);
      |         ~~~^~~~~~~~~~
a.cc:58:5: note: declared here
   58 | int dfs(int x, int p, int d) {
      |     ^~~
a.cc: In function 'int OneCentroid(int, const std::vector<std::vector<int> >&, const std::vector<bool>&)':
a.cc:99:9: error: 'function' was not declared in this scope
   99 |         function<void (int, int)> get_sz = [&](int u, int prev) {
      |         ^~~~~~~~
a.cc:99:9: note: 'std::function' is defined in header '<functional>'; this is probably fixable by adding '#include <functional>'
a.cc:99:32: error: expression list treated as compound expression in functional cast [-fpermissive]
   99 |         function<void (int, int)> get_sz = [&](int u, int prev) {
      |                                ^
a.cc:99:18: error: expected primary-expression before 'void'
   99 |         function<void (int, int)> get_sz = [&](int u, int prev) {
      |                  ^~~~
a.cc:106:9: error: 'get_sz' was not declared in this scope
  106 |         get_sz(root, -1);
      |         ^~~~~~
a.cc:108:31: error: expression list treated as compound expression in functional cast [-fpermissive]
  108 |         function<int (int, int)> dfs = [&](int u, int prev) {
      |                               ^
a.cc:108:18: error: expected primary-expression before 'int'
  108 |         function<int (int, int)> dfs = [&](int u, int prev) {
      |                  ^~~
a.cc:116:19: error: too few arguments to function 'int dfs(int, int, int)'
  116 |         return dfs(root, -1);
      |                ~~~^~~~~~~~~~
a.cc:58:5: note: declared here
   58 | int dfs(int x, int p, int d) {
      |     ^~~