Code
#include<bits/stdc++.h>
#define N 2000008
using namespace std;
struct sj{
int to,next;
}a[N*2];
int size,head[N],n,k;
void in(int &x)
{
char ch=getchar();int f=1,w=0;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){w=w*10+ch-'0';ch=getchar();}
x=f*w; return;
}
void add(int x,int y)
{
a[++size].to=y;
a[size].next=head[x];
head[x]=size;
}
int dep[N],dst[N],st,res;
void dfs(int fr,int x)
{
dst[x]=dep[x]=dep[fr]+1;
int lm,rm;
lm=0; rm=19260817;
int ljs=0,rjs=0;
for(int i=head[x];i;i=a[i].next)
{
int tt=a[i].to;
if(tt==fr)continue;
dfs(x,tt);
if(dst[tt]-dep[x]>(st/2))
rm=min(rm,dst[tt]-dep[x]),rjs++;
else
lm=max(lm,dst[tt]-dep[x]),ljs++;
}
if(rm+lm>st)
res-=rjs,dst[x]=dep[x]+lm;
else
res-=(rjs-1),dst[x]=dep[x]+rm;
return;
}
bool judge()
{
dfs(-1,1);
return (res>=0);
}
int main()
{
freopen("game.in","r",stdin);
freopen("game.out","w",stdout);
in(n),in(k);
for(int i=1;i<n;i++)
{
int x,y;
in(x),in(y);
add(x,y);add(y,x);
}
int l=0,r=n,ans=n*2;
while(l<=r)
{
int mid=(l+r)/2; st=mid; res=k;
if(judge())r=mid-1,ans=min(ans,mid);
else l=mid+1;
}
cout<<ans<<endl;
return 0;
}