【poj2486】【Apple Tree】【树形dp】

Apple Tree

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 7893 Accepted: 2642

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

题解:这道题我开始想的是用f[i][j]表示在i节点的时候已经走了j步时的取到的最多的苹果数。但是我发现这样做对于两个在不同子树中节点的LCA到根节点的这一段距离会重复计算。

所以我们把需要动规方程所表示的状态换一下:f[i][j]表示到节点i的时候在i的子树中已经走了j步时的取到的最多的苹果。

我们再考虑怎样用子树去更新:因为这样的情况比较多,所以我们可以考虑用背包来解决这个问题。

但是我们需要注意一个问题:那就是题目中说可能会走到一个节点以后就不再往回走了,对于这种问题,我们还需要在改进一下动规方程:f[i][j][0]表示到节点i的时候在i的子树中已经走了j步而且不能再回到这个节点取到的最多的苹果;f[i][j][1]就表示可以回来的。

那么动规方程就是:

f[i][j][1]=max(f[i][j][1],f[i][j-t][1]+f[son][t-2][1])(表示走到子节点再走回来,在走向子节点喝回来的过程中需要走两条边,所以减去)

f[i][j][0]=max(f[i][j][0],f[i][j-t][1]+f[son][t-1][1])(表示从另外的字节点走到根节点之后,走到son节点,不回来,由于只是下去没有再回来,所以只是-1)

f[i][j][0]=max(f[i][j][0],f[i][j-t][1]+f[son][t-2][1])(表示从送节点回到根节点后,再从根节点向下走不回来的时候的最大值,因为这个时候跟第一种一样来回总共走了两次,所以-2)

这样以后这个问题我们就可以解决了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int N=110;
const int K=210;
vector <int> tr[N];
int t,n,k,a[N],f[N][K][2];
void dp(int x,int last)
{
    int i,j,u,l;
    for(i=0;i<tr[x].size();++i){
        u=tr[x][i];
        if(u==last) continue;
        dp(tr[x][i],x);
        for(j=k;j>=1;--j){
            for(l=1;l<=j;++l){
                f[x][j][1]=max(f[x][j][1],f[x][j-l][1]+f[u][l-2][1]);
                f[x][j][0]=max(f[x][j][0],f[x][j-l][1]+f[u][l-1][0]);
                f[x][j][0]=max(f[x][j][0],f[x][j-l][0]+f[u][l-2][1]);
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&k)==2){
        int i,j,x,y;
        for(i=1;i<N;++i) tr[i].clear();
        memset(f,0,sizeof(f));
        for(i=1;i<=n;++i){
            scanf("%d",&a[i]);
            for(j=0;j<=k;++j){
                f[i][j][0]=f[i][j][1]=a[i];
            }
        }
        for(i=1;i<n;++i){
            scanf("%d%d",&x,&y);
            tr[x].push_back(y);
            tr[y].push_back(x);
        }
        dp(1,0);
        printf("%d\n",max(f[1][k][0],f[1][k][1]));
    }
}
时间: 2024-10-23 09:37:56

【poj2486】【Apple Tree】【树形dp】的相关文章

URAL_1018 Binary Apple Tree 树形DP+背包

这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过程中,有点难表示转移 后来看了下大神的做法才知道其实可以用背包来模拟 树枝的去留,其实真的是个背包诶,每个子树枝就相当于物品,他占用了多少树枝量,带来多少的收益,就是用背包嘛,于是用树形DP+背包就可以做了 #include <iostream> #include <cstdio> #

URAL 1018 Binary Apple Tree 树形DP 好题 经典

1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a binary tree, i.e. any biparous branch splits up to exactly two new branches. We will enu

【POJ 2486】 Apple Tree (树形DP)

Apple Tree 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 apple

POJ 2486 Apple Tree (树形dp 经典题)

Apple Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7784   Accepted: 2603 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 amoun

POJ 2486 Apple Tree 树形DP+分组背包

链接:http://poj.org/problem?id=2486 题意:一棵(苹果)树,树上有N个结点(N<=100),起点是结点1.每个结点上有若干个苹果,我可以进行K步操作(K<=200),每次操作是从当前结点移动到相邻的结点,并且到了相邻的结点以后会吃掉上面的所有苹果并且苹果不再长出来,相邻是指两个结点之间有边相连.问在K步操作之后最多可以吃掉多少个苹果. 思路:刚入手的时候觉得是一般的树形背包问题,dp[i][j]代表的是以i为根的子树中走j个结点所能吃到的苹果数,来进行状态转移,但

POJ 2486 Apple Tree (树形dp)

这是一个树上的背包转移.注意要用dp[i][j][k]表示第i个节点用了j的路程是否回到i节点,k=0表示回到i点,k=1表示不回到i点.那么实际上就是树上的一个背包转移. #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <string> #include <cctype> #include

Bestcoder round #65 &amp;&amp; hdu 5593 ZYB&#39;s Tree 树形dp

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 354    Accepted Submission(s): 100 Problem Description ZYB has a tree with N nodes,now he wants you to solve the numbers of nodes distanced no m

codeforces161D - Distance in Tree 树形dp

题意:给你一棵树,问你树中距离为k的有多少种情况. 解题思路:树形dp  维护每个节点(1-K)深度的情况, 解题代码: 1 // File Name: 161d.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月03日 星期日 19时20分10秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #incl

hdu5593/ZYB&#39;s Tree 树形dp

ZYB's Tree Memory Limit: 131072/131072 K (Java/Others) 问题描述 ZYBZYB有一颗NN个节点的树,现在他希望你对于每一个点,求出离每个点距离不超过KK的点的个数. 两个点(x,y)(x,y)在树上的距离定义为两个点树上最短路径经过的边数, 为了节约读入和输出的时间,我们采用如下方式进行读入输出: 读入:读入两个数A,BA,B,令fa_ifa?i??为节点ii的父亲,fa_1=0fa?1??=0;fa_i=(A*i+B)\%(i-1)+1fa

POJ2486 Apple Tree

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