Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu

Description

Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each node has an amount of apples. Wshxzt starts her happy trip at one node. She can eat up all the apples in the nodes she reaches. HX is a kind guy. He knows that eating too many can make the lovely girl become fat. So he doesn’t allow Wshxzt to go more than K steps in the tree. It costs one step when she goes from one node to another adjacent node. Wshxzt likes apple very much. So she wants to eat as many as she can. Can you tell how many apples she can eat in at most K steps.

Input

There are several test cases in the input 
Each test case contains three parts. 
The first part is two numbers N K, whose meanings we have talked about just now. We denote the nodes by 1 2 ... N. Since it is a tree, each node can reach any other in only one route. (1<=N<=100, 0<=K<=200) 
The second part contains N integers (All integers are nonnegative and not bigger than 1000). The ith number is the amount of apples in Node i. 
The third part contains N-1 line. There are two numbers A,B in each line, meaning that Node A and Node B are adjacent. 
Input will be ended by the end of file.

Note: Wshxzt starts at Node 1.

Output

For each test case, output the maximal numbers of apples Wshxzt can eat at a line.

Sample Input

2 1
0 11
1 2
3 2
0 1 2
1 2
1 3

Sample Output

11
2

Source

POJ Contest,Author:magicpig@ZSU

若当前走到了结点x,已经走了y步,除了走向子树外,还有另一选择:返回父节点,去父节点的其他子树。

所以比一般的树状DP多加一维状态,记录有没有返回当前结点。0表示要回,1表示不回。

懒得写分析了,复制一份233

dp[root][j][0] = MAX (dp[root][j][0] , dp[root][j-k][0] + dp[son][k-2][0]);//从s出发,要回到s,需要多走两步s-t,t-s,分配给t子树k步,其他子树j-k步,都返回

dp[root][j]][1] = MAX(  dp[root][j][1] , dp[root][j-k][0] + dp[son][k-1][1]) ;//先遍历s的其他子树,回到s,遍历t子树,在当前子树t不返回,多走一步

dp[root][j][1] = MAX (dp[root][j][1] , dp[root][j-k][1] + dp[son][k-2][0]);//不回到s(去s的其他子树),在t子树返回,同样有多出两步

by键盘上的舞者

我实际写的时候0和1与上面说的相反。

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
struct edge{
int v;
int nxt;
}e[mxn];
int hd[mxn],mct=;
void add_edge(int u,int v){
e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct;
return;
}
int f[mxn][mxn][];//第三维0表示不返回根节点,1表示返回根节点
int w[mxn];
int n,m;
void dp(int u,int fa){
int i,j,k;
for(i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==fa)continue;
dp(v,u);
for(j=m;j;--j){
for(k=;k<=j;++k){
f[u][j][]=max(f[u][j][],f[u][j-k][]+f[v][k-][]);
f[u][j][]=max(f[u][j][],f[u][j-k][]+f[v][k-][]);
f[u][j][]=max(f[u][j][],f[u][j-k][]+f[v][k-][]);
}
}
}
return;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
memset(e,,sizeof e);
memset(hd,,sizeof hd);
memset(f,,sizeof );
mct=;
int i,j;
for(i=;i<=n;i++){
scanf("%d",&w[i]);
for(j=;j<=m;j++){
f[i][j][]=f[i][j][]=w[i];
}
}
int u,v;
for(i=;i<n;i++){
scanf("%d%d",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
dp(,);
printf("%d\n",f[][m][]);
}
return ;
}

POJ2486 Apple Tree的更多相关文章

  1. POJ2486 Apple Tree(树形DP)

    题目大概是一棵树,每个结点都有若干个苹果,求从结点1出发最多走k步最多能得到多少个苹果. 考虑到结点可以重复走,容易想到这么个状态: dp[u][k][0]表示在以结点u为根的子树中走k步且必须返回u ...

  2. poj2486 Apple Tree (树形dp+分组背包)

    题目链接:https://vjudge.net/problem/POJ-2486 题意:一棵点权树,起点在1,求最多经过m条边的最大点权和. 思路: 树形dp经典题.用3维状态,dp[u][j][0/ ...

  3. POJ-2486 Apple Tree (树形DP)

    题目大意:一棵点带权有根树,根节点为1.从根节点出发,走k步,求能收集的最大权值和. 题目分析:从一个点向其某棵子树出发有三种可能的情况: 1.停留在那棵子树上: 2.再回到这个点: 3.经过这个点走 ...

  4. POJ2486 - Apple Tree(树形DP)

    题目大意 给定一棵n个结点的树,每个结点上有一定数量的苹果,你可以从结点1开始走k步(从某个结点走到相邻的结点算一步),经过的结点上的苹果都可以吃掉,问你最多能够吃到多少苹果? 题解 蛋疼的问题就是可 ...

  5. poj2486 Apple Tree (树形dp)

    题意:有一颗苹果树,树上的u节点上有num[u]个苹果,树根为1号节点,囧king从根开始走,没走到一个节点就把接点上的苹果吃光,问囧king在不超过k步的情况下最多吃多少个苹果. 解题思路:处理出两 ...

  6. poj2486 Apple Tree【区间dp】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4374766.html   ---by 墨染之樱花 [题目链接]http://poj.org/p ...

  7. POJ2486 Apple Tree 【树上背包】

    一句话题意:一棵树,一共n个点,每个点上有一个权值,求从1出发,走k步,最多能遍历到的权值.可以往回走. 第一(二)道树上背包题,先是看了dalao的题解,改了一点就过样例了.然而....TLE??? ...

  8. POJ2486 Apple Tree(树形背包)

    从每个节点u出发后有两种情况:回到u和不回到u. dp数组设为三维,第一维是节点编号,第二维是从该节点开始走的步数,第三维1/0 表示是否回到该节点. 可以回到时:dp[u][j][1]=max(dp ...

  9. 【树形dp】Apple Tree

    [poj2486]Apple Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10800   Accepted: 3 ...

随机推荐

  1. ios invalid put policy encoding 七牛上传报错

    获取七牛token的时候deadline不能为NSString类型 NSDictionary *infoDic = @{@"scope":@"yangtao", ...

  2. 使用python查询天气

    python主代码 weather.py import urllib2 import json from city import city cityname = raw_input('你想查哪个城市的 ...

  3. c++ 中的函数调用中的参数传递

    概述 初学 \(c++\),一直搞不懂其参数传递方式.故找到一篇不错的文章:刘志华的深入探讨C++语言中参数传递问题.亲自实践一遍,并作此记录,以加深印象.     主要内容 本文主要分为五个小部分, ...

  4. PHP 头部utf-8

    只是自己用的一些存储,请各位看官大大勿怪. header("Content-Type: text/html;charset=utf-8"); 2019年04月10日

  5. UIControlEvent

    UIControlEventTouchDown           = 1 <<  0,      // 手指落在按钮的一瞬间触发UIControlEventTouchDownRepeat ...

  6. vue新手入坑之mounted和created的区别(生命周期)

    这几个月用vue框架新做了一个项目,也算是边学习边实践吧.学习中也看过一些别人的开源项目,起初对mounted和created有一些疑惑,查询相关资料发现,这和vue的生命周期有关,在此也就做一个总结 ...

  7. cache控制器取值从TCM/CACHE/FLASH

    cache 控制器顶层有4组接口,分别为输入I/D bus ,输出TCM bus /Cache bus /direct flash bus (走cache ID bus matrix) flash 控 ...

  8. 排序算法C语言实现——冒泡排序

    /*冒泡O(n^2)*//*原理:    比较相邻的元素.如果第一个比第二个大,就交换他们两个.    对每一对相邻元素做同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应该会是最大的数 ...

  9. javamail腾讯企业邮箱发送邮件

    此代码用的jar文件:mail.jar(1.4.5版本); 如果jdk用的是1.8版本会出现SSL错误:这个问题是jdk导致的,jdk1.8里面有一个jce的包,安全性机制导致的访问https会报错, ...

  10. Linux快捷键列表

    Linux快捷键列表 快捷键 功能描述 快捷键 功能描述 control+p 查询命令历史纪录的上一条命令 方向上键 查询命令历史纪录的上一条命令 control+n 查询命令历史纪录的下一条命令 方 ...