Language: C++

#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int inf = 0x3f3f3f3f,oo = inf;
#define pi 3.14159265358979323846
#define IL inline
#define RG register
#define rep(i,a,b) for(RG int i=(a);i<(b);++i)
#define Rep(i,a,b) for(RG int i=(a);i<=(b);++i)
#define Dep(i,a,b) for(RG int i=(a);i>=(b);--i)
#define pc putchar
#define gc getchar
IL ll read(){
    RG ll x=0;char f=0;RG char c=gc();
    for(;!isdigit(c);c=gc())f|=(c=='-');
    for(;isdigit(c);c=gc())x=(x<<1)+(x<<3)+(c^48);
    return f?-x:x;
}
IL double readdb(){
    RG double x=0,p=0.1;RG char f=0,c=gc();
    for(;!isdigit(c);c=gc())f|=(c=='-');
    for(;isdigit(c);c=gc())x=x*10+(c^48);
    if(c=='.')for(c=gc();isdigit(c);c=gc(),p/=10)x=x+(c^48)*p;
    return f?-x:x;
}
IL void write(ll x){if(x<0)x=-x,pc('-');if(x>=10)write(x/10);pc(x%10+'0');}
IL void writeln(ll x){write(x);pc('\n');}
IL void wri(ll x){write(x),pc(' ');}
IL void writeln(ll x,char c,ll y){write(x);pc(c);writeln(y);}
IL void writeln(ll x,char c,ll y,char d,ll z){write(x);pc(c);write(y);pc(d);writeln(z);}
#define debug(x) printf(#x" = %d\n",x);
#define rd() read()
#define rdb() readdb()
#define mem(x,v) memset(x,v,sizeof(x))
#define pb push_back
#define mp make_pair
#define sqr(x) ((x)*(x))
#define lowbit(x) ((x)&(-(x)))
#define fin(x) freopen(#x".in","r",stdin)
#define fout(x) freopen(#x".out","w",stdout)
#define y1 ____y1
#define hash __hash
#define union _union
#define ramdom ((double)rand() / RAND_MAX)
#define x first
#define y second
const int maxn = 2e7+233;
struct Edge{
	int to,nxt;
	Edge(){}
	Edge(int to,int nxt):to(to),nxt(nxt){}
}edge[maxn*2];
int first[maxn],nume;
void Addedge(int a,int b){
	edge[nume] = Edge(b,first[a]);
	first[a] = nume++;
}
int mxdep[maxn],wson[maxn],size[maxn];
int dfn[maxn],dfx[maxn],n,k;
int dp_array[maxn];
int *f[maxn];
void init(int u,int fa){
	mxdep[u] = 0;wson[u] = 0;size[u] = 1;
	for(int e=first[u];~e;e=edge[e].nxt){
		int v=edge[e].to;
		if(v == fa) continue;
		init(v,u);
		mxdep[u] = max(mxdep[u],mxdep[v] + 1);
		size[u] += size[v];
		if(!wson[u] || mxdep[v] > mxdep[wson[u]])
			wson[u] = v;
	}
}
void pre(int u,int fa){
	dfx[dfn[u] = ++*dfn] = u;
	f[u] = dp_array + dfn[u];
	for(int e=first[u];~e;e=edge[e].nxt){
		int v=edge[e].to;
		if(v==fa) continue;
		pre(v,u);
	}
}
int g[maxn],h[maxn],ans;
void dfs(int u,int fa){
	if(wson[u]) dfs(wson[u],u);
	f[u][0] = wson[u] ? size[wson[u]] : 0;
	//printf("%d %d\n",u,mxdep[u]);
	int m = min(k,mxdep[u]);
	for(int e=first[u];~e;e=edge[e].nxt){
		int v = edge[e].to;
		if(v==wson[u] || v==fa) continue;
		dfs(v,u);
		int len = min(k,mxdep[v] + 1);
		g[0] = size[v];Rep(i,1,len) g[i] = f[v][i-1];
		Rep(i,0,len) h[i] = f[u][i];
//		puts("~~"); 
//		wri(g[0]),writeln(h[0]);
		Rep(i,0,len) f[u][i] = min(g[i]+h[min(i,k-i)],g[min(i,k-i)] + h[i]);
		Rep(i,0,len) if(k-i>len && k-i<=m) f[u][k-i] += g[i];
		Rep(i,len+1,k-len-1) f[u][i] += g[len];
	}
//	Rep(i,0,m) wri(f[u][i]);puts("");
	ans = min(ans,n - size[u] + f[u][min(k,m)]);
	//从root往上搞一格
}
int main(){
	mem(first,-1);nume = 0;
	n = read();k = read();ans = n;
	rep(i,1,n) {
		int x = read(),y = read();
		Addedge(x,y);
		Addedge(y,x);
	} init(1,0);
	*dfn = 0;
	pre(1,0);
	dfs(1,0);
	writeln(ans);
	return 0;
}

Compiler output:
a.cc: In function 'll read()':
a.cc:18:11: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   18 |     RG ll x=0;char f=0;RG char c=gc();
      |           ^
a.cc:18:32: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   18 |     RG ll x=0;char f=0;RG char c=gc();
      |                                ^
a.cc: In function 'double readdb()':
a.cc:24:15: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   24 |     RG double x=0,p=0.1;RG char f=0,c=gc();
      |               ^
a.cc:24:19: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   24 |     RG double x=0,p=0.1;RG char f=0,c=gc();
      |                   ^
a.cc:24:33: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   24 |     RG double x=0,p=0.1;RG char f=0,c=gc();
      |                                 ^
a.cc:24:37: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   24 |     RG double x=0,p=0.1;RG char f=0,c=gc();
      |                                     ^
a.cc: In function 'void init(int, int)':
a.cc:67:34: error: reference to 'size' is ambiguous
   67 |         mxdep[u] = 0;wson[u] = 0;size[u] = 1;
      |                                  ^~~~
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,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:62:28: note:                 'int size [20000233]'
   62 | int mxdep[maxn],wson[maxn],size[maxn];
      |                            ^~~~
a.cc:73:17: error: reference to 'size' is ambiguous
   73 |                 size[u] += size[v];
      |                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:62:28: note:                 'int size [20000233]'
   62 | int mxdep[maxn],wson[maxn],size[maxn];
      |                            ^~~~
a.cc:73:28: error: reference to 'size' is ambiguous
   73 |                 size[u] += size[v];
      |                            ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:62:28: note:                 'int size [20000233]'
   62 | int mxdep[maxn],wson[maxn],size[maxn];
      |                            ^~~~
a.cc: In function 'void dfs(int, int)':
a.cc:90:29: error: reference to 'size' is ambiguous
   90 |         f[u][0] = wson[u] ? size[wson[u]] : 0;
      |                             ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:62:28: note:                 'int size [20000233]'
   62 | int mxdep[maxn],wson[maxn],size[maxn];
      |                            ^~~~
a.cc:98:24: error: reference to 'size' is ambiguous
   98 |                 g[0] = size[v];Rep(i,1,len) g[i] = f[v][i-1];
      |                        ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:62:28: note:                 'int size [20000233]'
   62 | int mxdep[maxn],wson[maxn],size[maxn];
      |                            ^~~~
a.cc:98:36: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   98 |                 g[0] = size[v];Rep(i,1,len) g[i] = f[v][i-1];
      |                                    ^
a.cc:13:31: note: in definition of macro 'Rep'
   13 | #define Rep(i,a,b) for(RG int i=(a);i<=(b);++i)
      |                               ^
a.cc:99:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   99 |                 Rep(i,0,len) h[i] = f[u][i];
      |                     ^
a.cc:13:31: note: in definition of macro 'Rep'
   13 | #define Rep(i,a,b) for(RG int i=(a);i<=(b);++i)
      |                               ^
a.cc:102:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  102 |                 Rep(i,0,len) f[u][i] = min(g[i]+h[min(i,k-i)],g[min(i,k-i)] + h[i]);
      |                     ^
a.cc:13:31: note: in definition of macro 'Rep'
   13 | #define Rep(i,a,b) for(RG int i=(a);i<=(b);++i)
      |                               ^
a.cc:103:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  103 |                 Rep(i,0,len) if(k-i>len && k-i<=m) f[u][k-i] += g[i];
      |                     ^
a.cc:13:31: note: in definition of macro 'Rep'
   13 | #define Rep(i,a,b) for(RG int i=(a);i<=(b);++i)
      |                               ^
a.cc:104:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  104 |                 Rep(i,len+1,k-len-1) f[u][i] += g[len];
      |                     ^
a.cc:13:31: note: in definition of macro 'Rep'
   13 | #define Rep(i,a,b) for(RG int i=(a);i<=(b);++i)
      |                               ^
a.cc:107:27: error: reference to 'size' is ambiguous
  107 |         ans = min(ans,n - size[u] + f[u][min(k,m)]);
      |                           ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:62:28: note:                 'int size [20000233]'
   62 | int mxdep[maxn],wson[maxn],size[maxn];
      |                            ^~~~
a.cc: In function 'int main()':
a.cc:113:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  113 |         rep(i,1,n) {
      |             ^
a.cc:12:31: note: in definition of macro 'rep'
   12 | #define rep(i,a,b) for(RG int i=(a);i<(b);++i)
      |                               ^