Language: C++

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

#define REP(i,n) for(int i=0;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF INT_MAX/3
#define LINF LLONG_MAX/3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(),(v).end()
#define debug(x) cout<<#x<<":"<<x<<endl
#define debug2(x,y) cout<<#x<<","<<#y":"<<x<<","<<y<<endl
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& vec){
    os << "[";
    for(const auto& v : vec){
        os << v << ",";
    }
    os << "]";
    return os;
}

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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int N,K;
    cin >> N >> K;
    vector<vector<int>> G(N,vector<int>(N));
    vector<int> A(N-1),B(N-1);
    rep(i,N-1){
        int a,b;cin>>a>>b;
        a--;
        b--;
        A[i]=a;
        B[i]=b;
        G[a].PB(b);
        G[b].PB(a);
    }

    if(K%2==0){
        int ans = INF;
        rep(i,N){
            // 点iからの距離を求める
            queue<int> que;
            vector<int> d(N,INF);
            d[i]=0;
            que.push(i);
            int tt=0;
            while(!que.empty()){
                tt++;
                if(tt>4000) assert(false);
                int q = que.front();
                que.pop();
                assert(d[q]!|=INF);
                rep(j,G[q].size()){
                    int nx = G[q][j];
                    if(d[nx]!=INF) continue;
                    d[nx] = d[q]+1;
                    que.push(nx);
                }
            }
            int cnt=0;
            rep(j,N) if(d[j]>K/2) cnt++;
            //cerr << i << endl;
            //cerr << d << endl;
            ans = min(ans,cnt);
        }
        cout << ans << endl;
    }else{
        int ans=INF;
        rep(i,N-1){
            int s=A[i],t=B[i];
            vector<int> ds(N,INF);
            queue<int> que;
            que.push(s);
            ds[s]=0;
            int tt=0;
            while(!que.empty()){
                tt++;
                if(tt>4000) assert(false);
                int q = que.front();
                que.pop();
                rep(j,G[q].size()){
                    int nx = G[q][j];
                    if(ds[nx]!=INF) continue;
                    ds[nx]=ds[q]+1;
                    que.push(nx);
                }
            }

            vector<int> dt(N,INF);
            dt[t]=0;
            que.push(t);
            tt=0;
            while(!que.empty()){
                tt++;
                if(tt>4000) assert(false);
                int q = que.front();
                que.pop();
                rep(j,G[q].size()){
                    int nx=G[q][j];
                    if(dt[nx]!=INF) continue;
                    dt[nx]=dt[q]+1;
                    que.push(nx);
                }
            }
            int cnt=0;
            rep(j,N){
                int d=min(ds[j],dt[j]);
                if(d>(K-1)/2) cnt++;
            }
            ans = min(ans,cnt);
        }
        cout << ans << endl;
    }
}


Compiler output:
In file included from /usr/include/c++/14/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:106,
                 from a.cc:1:
a.cc: In function 'int main()':
a.cc:62:28: error: expected ')' before '!' token
   62 |                 assert(d[q]!|=INF);
      |                            ^
a.cc:62:28: error: expected ')' before '!' token
a.cc:62:17: note: to match this '('
   62 |                 assert(d[q]!|=INF);
      |                 ^~~~~~