ZOJ 3188 ZOJ 3188 Treeland Exhibition(树形DP)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3278
题意:给出一棵树,找出一个不大于长度为m的链,使得其他点到该链的长度之和最小。
预处理出以下数组:
(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<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
int ans,downcnt[],downsum[];
int upsum[],upcnt[];
int n,m;
int tot,go[],first[],next[];
int dp[][],f[][];
void insert(int x,int y){
tot++;
go[tot]=y;
next[tot]=first[x];
first[x]=tot;
}
void add(int x,int y){
insert(x,y);insert(y,x);
}
void dfs(int x,int fa){
downcnt[x]=;
downsum[x]=;
upcnt[x]=;
upsum[x]=;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa) continue;
dfs(pur,x);
downcnt[x]+=downcnt[pur];
downsum[x]+=downsum[pur]+downcnt[pur];
}
upcnt[x]=n-downcnt[x];
}
void Dfs(int x,int fa){
if (fa!=){
upcnt[x]=n-downcnt[x];
upsum[x]=upsum[fa]+upcnt[fa]+downsum[fa]-downsum[x]-downcnt[fa]++(downcnt[fa]--downcnt[x])*+;
}else upsum[x]=upcnt[x]=;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa) continue;
Dfs(pur,x);
}
}
void DP(int x,int fa){
for (int i=;i<=m+;i++)
dp[x][i]=downsum[x],f[x][i]=downsum[x]+upsum[x];
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa) continue;
DP(pur,x);
for (int j=;j<=m;j++){
dp[x][j]=std::min(dp[x][j],dp[pur][j-]+downsum[x]-downsum[pur]-downcnt[pur]);
if (m-j->=) ans=std::min(ans,f[x][j]+dp[pur][m--j]-downsum[pur]-downcnt[pur]);
}
for (int j=;j<=m;j++){
f[x][j]=std::min(f[x][j],upsum[x]+downsum[x]+dp[pur][j-]-downsum[pur]-downcnt[pur]);
}
}
ans=std::min(ans,dp[x][m]+upsum[x]);
}
int main(){
while (scanf("%d%d",&n,&m)!=EOF){
if (n==&&m==) return ;
tot=;for (int i=;i<=n;i++) first[i]=;
for (int i=;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
x++;y++;
add(x,y);
}
ans=0x7fffffff;
dfs(,);
Dfs(,);
DP(,);
printf("%d\n",ans);
}
}
ZOJ 3188 ZOJ 3188 Treeland Exhibition(树形DP)的更多相关文章
- CF 219 D:Choosing Capital for Treeland(树形dp)
D. Choosing Capital for Treeland 链接:http://codeforces.com/problemset/problem/219/D The country Tre ...
- Codeforces 219D Choosing Capital for Treeland(树形DP)
题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正 ...
- ZOJ 3949 Edge to the Root( 树形dp)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3949 题解:树dp真的很直觉,或者说dp真的很直觉.就上周末比赛时其实前一 ...
- ZOJ 3626 Treasure Hunt I(树形dp)
Treasure Hunt I Time Limit: 2 Seconds Memory Limit: 65536 KB Akiba is a dangerous country since ...
- ZOJ 3626 Treasure Hunt I (树形DP,常规)
题意:给一棵树,一个人站在节点s,他有m天时间去获取各个节点上的权值,并且最后需要回到起点s,经过每条边需要消耗v天,问最少能收获多少权值? 思路: 常规的,注意还得跑回原地s. //#include ...
- 【codeforce 219D】 Choosing Capital for Treeland (树形DP)
Choosing Capital for Treeland Description The country Treeland consists of n cities, some pairs of t ...
- Codeforces 490F Treeland Tour 树形dp
Treeland Tour 离散化之后, 每个节点维护上升链和下降链, 感觉复杂度有点高, 为啥跑这么快.. #include<bits/stdc++.h> #define LL long ...
- CodeForces 219D Choosing Capital for Treeland (树形DP)
题意:给一个树形图,n个节点,n-1条有向边,要求选一个节点作为根,使需要改变方向的边的数目最少.并输出所有可能作为根的点. 思路: 先随便一个点进行DFS,计算将每棵子树的边全部往下时,所需要的费用 ...
- Choosing Capital for Treeland CodeForces - 219D (树形DP)
传送门 The country Treeland consists of n cities, some pairs of them are connected with unidirectional ...
随机推荐
- 用keil直接生成BIN文件
1.下载hex2bin到keil的任意目录,下载地址:http://www.keil.com/download/docs/7.asp 或者 http://www.hex2bin.com/files/h ...
- FreeBSD ipfw App配置Examples
Step # 1: Enabling IPFW Open /etc/rc.conf file # vi /etc/rc.conf Append following settings: firewall ...
- n%i之和
题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1168 题意:给定一个n,注意这里n小于10^12,求 分析:早些时 ...
- iOS开发:深入理解GCD 第一篇
最近把其他书籍都放下了,主要是在研究GCD.如果是为了工作,以我以前所学的GCD.NSOperation等知识已经足够用了,但学习并不仅仅知识满足于用它,要知其然.并且知其所以然,这样才可以不断的提高 ...
- git 技巧
将某个文件回退到某个版本 git co d359624286d9c1f022b8b3b6f2d3fe3b6524188b build.sh 查看某个文件在某个版本时的内容 git show d3596 ...
- ListView的Item被点击和其中的Button被点击同时生效
Android开发中在ListView中经常有Button或ImageButton等需要被点击的控件,如果不加一些特殊的限制,有可能 ListView的Item的点击事件或Button的点击事件,其中 ...
- eclipse注释模板修改
http://swiftlet.net/archives/1199 以下为模板文件 <?xml version="1.0" encoding="UTF-8" ...
- RMAN常用备份恢复命令汇总
RMAN命令 1.独立命令 RMAN>shutdown immediate RMAN>startup RMAN>backup format 'd:\backup\%d_%s.b ...
- jquery,js常用特效名称
- jquery悬停tab
<style> *{ margin:0; padding:0;} body { font:12px/19px Arial, Helvetica, sans-serif; color:#66 ...