题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3278
题意:给出一棵树,找出一个不大于长度为m的链,使得其他点到该链的长度之和最小。
思路:首先,链有两种。对于一个子树u,或者u一直向下成竖线形状,或者以u为转折点以及u的两个孩子组成的形状。首先预处理出以下数组:
(1)downSum[u]:u的所有孩子到达u的距离,downCnt[u]:u子树节点个数;
(2)upSum[u]:除u子树的节点外其他节点到达u的距离;upCnt[u]:除u子树的节点个数。
树形DP时,竖线形状的比较简单,我们用dp[u][L]表示以u为子树,向下有一条长度为L的链的最小代价;然后dp[u][m]+upSum[u]就是答案;对于折线形状的,设以u的两个孩子v1、v2组成,则答案为upSum[u]+downSum[u]+dp[v1][j-1]-downSum[v1]-downCnt[v1]+dp[v2][m-1-j]-downSum[v2]-downCnt[v2],因此只要维护upSum[u]+downSum[u]+dp[v1][j-1]-downSum[v1]-downCnt[v1]的最小值即可。
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <ctype.h>
#include <time.h>
#define abs(x) ((x)>=0?(x):-(x))
#define i64 long long
#define u32 unsigned int
#define u64 unsigned long long
#define clr(x,y) memset(x,y,sizeof(x))
#define CLR(x) x.clear()
#define ph(x) push(x)
#define pb(x) push_back(x)
#define Len(x) x.length()
#define SZ(x) x.size()
#define PI acos(-1.0)
#define sqr(x) ((x)*(x))
#define MP(x,y) make_pair(x,y)
#define EPS 1e-10
#define FOR0(i,x) for(i=0;i<x;i++)
#define FOR1(i,x) for(i=1;i<=x;i++)
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FORL0(i,a) for(i=a;i>=0;i--)
#define FORL1(i,a) for(i=a;i>=1;i--)
#define FORL(i,a,b)for(i=a;i>=b;i--)
#define rush() int CC;for(scanf("%d",&CC);CC--;)
#define Rush(n) while(scanf("%d",&n)!=-1)
using namespace std;
void RD(int &x){scanf("%d",&x);}
void RD(i64 &x){scanf("%I64d",&x);}
void RD(u64 &x){scanf("%I64u",&x);}
void RD(u32 &x){scanf("%u",&x);}
void RD(double &x){scanf("%lf",&x);}
void RD(int &x,int &y){scanf("%d%d",&x,&y);}
void RD(i64 &x,i64 &y){scanf("%I64d%I64d",&x,&y);}
void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}
void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}
void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}
void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}
void RD(i64 &x,i64 &y,i64 &z){scanf("%I64d%I64d%I64d",&x,&y,&z);}
void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}
void RD(char &x){x=getchar();}
void RD(char *s){scanf("%s",s);}
void RD(string &s){cin>>s;}
void PR(int x) {printf("%d\n",x);}
void PR(int x,int y) {printf("%d %d\n",x,y);}
void PR(i64 x) {printf("%lld\n",x);}
void PR(i64 x,i64 y) {printf("%I64d %I64d\n",x,y);}
void PR(u32 x) {printf("%u\n",x);}
void PR(u64 x) {printf("%I64u\n",x);}
void PR(double x) {printf("%.6lf\n",x);}
void PR(double x,double y) {printf("%.5lf %.5lf\n",x,y);}
void PR(char x) {printf("%c\n",x);}
void PR(char *x) {printf("%s\n",x);}
void PR(string x) {cout<<x<<endl;}
void upMin(int &x,int y) {if(x>y) x=y;}
void upMin(i64 &x,i64 y) {if(x>y) x=y;}
void upMin(double &x,double y) {if(x>y) x=y;}
void upMax(int &x,int y) {if(x<y) x=y;}
void upMax(i64 &x,i64 y) {if(x<y) x=y;}
void upMax(double &x,double y) {if(x<y) x=y;}
const int mod=10000;
const double dinf=1e12;
const i64 inf=((i64)1)<<60;
const int INF=1000000000;
const int N=10005;
int n,m;
vector<int> g[N];
int downSum[N],upSum[N],downCnt[N],upCnt[N];
void DFS1(int u,int pre)
{
downCnt[u]=1;
downSum[u]=0;
int i,v;
FOR0(i,SZ(g[u]))
{
v=g[u][i];
if(v==pre) continue;
DFS1(v,u);
downCnt[u]+=downCnt[v];
downSum[u]+=downSum[v]+downCnt[v];
}
}
void DFS2(int u,int pre)
{
if(pre!=-1)
{
upCnt[u]=n-downCnt[u];
upSum[u]=upSum[pre]+upCnt[pre]+downSum[pre]-downSum[u]-(downCnt[pre]-1)+(downCnt[pre]-1-downCnt[u])*2+1;
}
else upCnt[u]=upSum[u]=0;
int i,v;
FOR0(i,SZ(g[u]))
{
v=g[u][i];
if(v==pre) continue;
DFS2(v,u);
}
}
int dp[N][105],f[N][105];
int ans;
void DFS(int u,int pre)
{
int i,j,v;
FOR0(i,m+1) dp[u][i]=downSum[u],f[u][i]=downSum[u]+upSum[u];
FOR0(i,SZ(g[u]))
{
v=g[u][i];
if(v==pre) continue;
DFS(v,u);
for(j=1;j<=m;j++)
{
upMin(dp[u][j],dp[v][j-1]+downSum[u]-downSum[v]-downCnt[v]);
if(m-1-j>=0) upMin(ans,f[u][j]+dp[v][m-1-j]-downSum[v]-downCnt[v]);
}
for(j=1;j<=m;j++) upMin(f[u][j],upSum[u]+downSum[u]+dp[v][j-1]-downSum[v]-downCnt[v]);
}
upMin(ans,dp[u][m]+upSum[u]);
}
int main()
{
Rush(n)
{
RD(m);
if(n==0&&m==0) break;
int i;
FOR0(i,n) g[i].clear();
int x,y;
FOR1(i,n-1)
{
RD(x,y);
g[x].pb(y);
g[y].pb(x);
}
DFS1(0,-1); DFS2(0,-1);
ans=INF; DFS(0,-1);
PR(ans);
}
}