Language: C++

#include<bits/stdc++.h>
#define LF double
#define LL long long
#define ULL unsigned int
#define fo(i,j,k) for(int i=j;i<=k;i++)
#define fd(i,j,k) for(int i=j;i>=k;i--)
#define fr(i,j) for(int i=begin[j];i;i=next[i])
using namespace std;
int const mn=2*1e3+3,mo=1e9+7;
int n,K,tmp,u[mn],v[mn],gra,begin[mn],to[mn*2],next[mn*2];
void insert(int u,int v){
    to[++gra]=v;
    next[gra]=begin[u];
    begin[u]=gra;
}
void dfs(int p,int q,int dep){
    tmp++;
    if(!dep)return;
    fr(i,p)if(to[i]!=q)
        dfs(to[i],p,dep-1);
}
int main(){
    scanf("%d%d",&n,&K);
    fo(i,1,n-1){
        scanf("%d%d",&u[i],&v[i]);
        insert(u[i],v[i]);
        insert(v[i],u[i]);
    }
    int ans=0;
    if(K&1){
        fo(i,1,n-1){
            tmp=0;
            dfs(u[i],v[i],K/2);
            dfs(v[i],u[i],K/2);
            ans=max(ans,tmp);
        }
    }else{
        fo(i,1,n){
            tmp=0;
            dfs(i,0,K/2);
            ans=max(ans,tmp);
        }
    }
    printf("%d",n-ans);
    return 0;
}

Compiler output:
a.cc: In function 'void insert(int, int)':
a.cc:13:5: error: reference to 'next' is ambiguous
   13 |     next[gra]=begin[u];
      |     ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:10:48: note:                 'int next [4006]'
   10 | int n,K,tmp,u[mn],v[mn],gra,begin[mn],to[mn*2],next[mn*2];
      |                                                ^~~~
a.cc:13:15: error: reference to 'begin' is ambiguous
   13 |     next[gra]=begin[u];
      |               ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:166:
/usr/include/c++/14/valarray:1238:5: note: candidates are: 'template<class _Tp> const _Tp* std::begin(const valarray<_Tp>&)'
 1238 |     begin(const valarray<_Tp>& __va) noexcept
      |     ^~~~~
/usr/include/c++/14/valarray:1227:5: note:                 'template<class _Tp> _Tp* std::begin(valarray<_Tp>&)'
 1227 |     begin(valarray<_Tp>& __va) noexcept
      |     ^~~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52:
/usr/include/c++/14/bits/range_access.h:95:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::begin(_Tp (&)[_Nm])'
   95 |     begin(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:63:5: note:                 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(const _Container&)'
   63 |     begin(const _Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:52:5: note:                 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(_Container&)'
   52 |     begin(_Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
In file included from /usr/include/c++/14/bits/algorithmfwd.h:39,
                 from /usr/include/c++/14/bits/stl_algo.h:59,
                 from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/initializer_list:88:5: note:                 'template<class _Tp> constexpr const _Tp* std::begin(initializer_list<_Tp>)'
   88 |     begin(initializer_list<_Tp> __ils) noexcept
      |     ^~~~~
a.cc:10:29: note:                 'int begin [2003]'
   10 | int n,K,tmp,u[mn],v[mn],gra,begin[mn],to[mn*2],next[mn*2];
      |                             ^~~~~
a.cc:14:5: error: reference to 'begin' is ambiguous
   14 |     begin[u]=gra;
      |     ^~~~~
/usr/include/c++/14/valarray:1238:5: note: candidates are: 'template<class _Tp> const _Tp* std::begin(const valarray<_Tp>&)'
 1238 |     begin(const valarray<_Tp>& __va) noexcept
      |     ^~~~~
/usr/include/c++/14/valarray:1227:5: note:                 'template<class _Tp> _Tp* std::begin(valarray<_Tp>&)'
 1227 |     begin(valarray<_Tp>& __va) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:95:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::begin(_Tp (&)[_Nm])'
   95 |     begin(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:63:5: note:                 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(const _Container&)'
   63 |     begin(const _Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:52:5: note:                 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(_Container&)'
   52 |     begin(_Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
/usr/include/c++/14/initializer_list:88:5: note:                 'template<class _Tp> constexpr const _Tp* std::begin(initializer_list<_Tp>)'
   88 |     begin(initializer_list<_Tp> __ils) noexcept
      |     ^~~~~
a.cc:10:29: note:                 'int begin [2003]'
   10 | int n,K,tmp,u[mn],v[mn],gra,begin[mn],to[mn*2],next[mn*2];
      |                             ^~~~~
a.cc: In function 'void dfs(int, int, int)':
a.cc:7:27: error: reference to 'begin' is ambiguous
    7 | #define fr(i,j) for(int i=begin[j];i;i=next[i])
      |                           ^~~~~
a.cc:19:5: note: in expansion of macro 'fr'
   19 |     fr(i,p)if(to[i]!=q)
      |     ^~
/usr/include/c++/14/valarray:1238:5: note: candidates are: 'template<class _Tp> const _Tp* std::begin(const valarray<_Tp>&)'
 1238 |     begin(const valarray<_Tp>& __va) noexcept
      |     ^~~~~
/usr/include/c++/14/valarray:1227:5: note:                 'template<class _Tp> _Tp* std::begin(valarray<_Tp>&)'
 1227 |     begin(valarray<_Tp>& __va) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:95:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::begin(_Tp (&)[_Nm])'
   95 |     begin(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:63:5: note:                 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(const _Container&)'
   63 |     begin(const _Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:52:5: note:                 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(_Container&)'
   52 |     begin(_Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
/usr/include/c++/14/initializer_list:88:5: note:                 'template<class _Tp> constexpr const _Tp* std::begin(initializer_list<_Tp>)'
   88 |     begin(initializer_list<_Tp> __ils) noexcept
      |     ^~~~~
a.cc:10:29: note:                 'int begin [2003]'
   10 | int n,K,tmp,u[mn],v[mn],gra,begin[mn],to[mn*2],next[mn*2];
      |                             ^~~~~
a.cc:7:40: error: reference to 'next' is ambiguous
    7 | #define fr(i,j) for(int i=begin[j];i;i=next[i])
      |                                        ^~~~
a.cc:19:5: note: in expansion of macro 'fr'
   19 |     fr(i,p)if(to[i]!=q)
      |     ^~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:10:48: note:                 'int next [4006]'
   10 | int n,K,tmp,u[mn],v[mn],gra,begin[mn],to[mn*2],next[mn*2];
      |                                                ^~~~