Language: C++

#include <iostream>
#include <vector>
using namespace std;

int solve(int root, int N, int K, const vector<vector<int>> &Es){
    if (K == 1) return N - 2;
    if (K == 2){
        int ans = 0;
        for (const auto &e : Es) ans = max(ans, e.size());
        return N - ans - 1;
    }
    int k = K / 2;
    vector<int> depth(N, -1);
    depth[root] = 0;
    vector<int> parent(N, -1);
    parent[root] = root;
    vector<int> child(N, 0);
    vector<int> childKp1(N, 0);
    vector<bool> closed(N, false);
    vector<int> stack = {root};
    while (not stack.empty()){
        int v = stack.back();
        if (closed[v]){
            stack.pop_back();
            childKp1[parent[v]] += childKp1[v];
            child[parent[v]] += child[v] + 1;
            continue;
        }
        for (int u : Es[v]){
            if (depth[u] != -1) continue;
            depth[u] = depth[v] + 1;
            parent[u] = v;
            if (depth[u] == k + 1){
                ++childKp1[v];
                continue;
            }
            stack.push_back(u);
        }
        closed[v] = true;
    }
    int cum = 1;
    int maxKp1 = 0;
    for (int v : Es[root]){
        cum += child[v] + 1;
        maxKp1 = max(maxKp1, childKp1[v]);
    }
    int ans = N - cum;
    if (K & 1) ans -= maxKp1;
    return ans;
}


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, K;
    cin >> N >> K;
    vector<vector<int>> Es(N, vector<int>());
    for (int i = 0; i != N - 1; ++i){
        int a, b;
        cin >> a >> b;
        Es[a - 1].push_back(b - 1);
        Es[b - 1].push_back(a - 1);
    }
    int ans = N;
    for (int v = 0; v != N; ++v){
        ans = min(ans, solve(v, N, K, Es));
    }
    cout << ans << endl;
    return 0;
}

Compiler output:
a.cc: In function 'int solve(int, int, int, const std::vector<std::vector<int> >&)':
a.cc:9:43: error: no matching function for call to 'max(int&, std::vector<int>::size_type)'
    9 |         for (const auto &e : Es) ans = max(ans, e.size());
      |                                        ~~~^~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:9:43: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'std::vector<int>::size_type' {aka 'long unsigned int'})
    9 |         for (const auto &e : Es) ans = max(ans, e.size());
      |                                        ~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided