设f[i][j]为由i号点开始在子树内走j步最多能经过多少格点,g[i][j]为由i号点开始在子树内走j步且回到i最多能经过多少格点,转移显然。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 110
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<''||c>'')) c=getchar();return c;}
int gcd(int n,int m){return m==?n:gcd(m,n%m);}
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
int n,m,p[N],f[N][N],g[N][N],t;
struct data{int to,nxt;
}edge[N<<];
void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;}
void dfs(int k,int from)
{
f[k][]=g[k][]=;for (int i=;i<=m;i++) f[k][i]=g[k][i]=-n;
for (int i=p[k];i;i=edge[i].nxt)
if (edge[i].to!=from)
{
dfs(edge[i].to,k);
for (int x=m;x>=;x--)
for (int y=;y+<=x;y++)
f[k][x]=max(f[k][x],max(g[k][x-y-]+f[edge[i].to][y],(x-y->=?f[k][x-y-]+g[edge[i].to][y]:-n)));
for (int x=m;x>=;x--)
for (int y=;y+<=x;y++)
g[k][x]=max(g[k][x],g[k][x-y-]+g[edge[i].to][y]);
} }
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj4813.in","r",stdin);
freopen("bzoj4813.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
n=read(),m=read();
for (int i=;i<n;i++)
{
int x=read()+,y=read()+;
addedge(x,y),addedge(y,x);
}
dfs(,);
for (int i=;i<m;i++) f[][m]=max(f[][m],f[][i]);
cout<<f[][m];
return ;
}