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. Java代理设计模式(Proxy)的四种具体实现:静态代理和动态代理

    面试问题:Java里的代理设计模式(Proxy Design Pattern)一共有几种实现方式?这个题目很像孔乙己问"茴香豆的茴字有哪几种写法?" 所谓代理模式,是指客户端(Cl ...

  2. IOS Array 排序方法

    NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) { if ([obj1 integerVal ...

  3. Qt+事件的接收和忽略

    事件的接收与忽略的示意图如下图: 依据前面的知识,事件是可以依据情况进行接收和忽略的,事件的传播是组件层次上面的,而不是依靠类继承机制.在一个特殊的情形下,我们必须使用accept()和ignore( ...

  4. [BZOJ4899]:记忆的轮廓(概率DP)

    题目传送门 题目描述: 通往贤者之塔的路上,有许多的危机. 我们可以把这个地形看做是一颗树,根节点编号为1,目标节点编号为n,其中1-n的简单路径上,编号依次递增, 在[1,n]中,一共有n个节点.我 ...

  5. react native 在window 7上配置开发环境-Andorid

    参照官方配置:https://facebook.github.io/react-native/docs/getting-started.html 因为在配置的过程中遇到很多问题,在此记录一下. 1.j ...

  6. Hello World投票以太坊Dapp教程-Part1

    参考资料:https://medium.com/@mvmurthy/full-stack-hello-world-voting-ethereum-dapp-tutorial-part-1-40d2d0 ...

  7. strchr函数

    函数原型:extern char *strchr(char *str,char character) 参数说明:str为一个字符串的指针,character为一个待查找字符.         所在库名 ...

  8. shelll脚本,根据软链接,找到真实路径

    [root@localhost tmp]# ls -l total lrwxrwxrwx root root Sep : abc -> /etc/passwd lrwxrwxrwx root r ...

  9. css布局--两列布局,左侧固定,右侧自适应(其中左侧要可以拖动,右侧水平滚动条)

    (css布局所要实现的效果) 在前端面试中经常会被问到CSS布局,两列布局,左侧固定,右侧自适应.前几天去面试,遇到了这道题的升级版,要求左侧可拖动,右侧要有水平滚动条.拿到题目确实有些大脑短路,不知 ...

  10. Django ORM操作及进阶

    一般操作 看专业的官网文档,做专业的程序员! 必知必会13条 <1> all(): 查询所有结果 <2> filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 ...