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

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
 
题目大意:从N个点的树的根(1)出发,走V步,每个点都有一个权值,问能经过的最大权值和是多少?
试题分析:树形dp一般情况下是有一颗树,询问这棵树的某些最优性信息或者方案数信息。
     本题有N与V所以至少有两个状态:dp[N][V];
     考虑dp[i][j]的转移:dp[i][j]=max(dp[i->son][v-1])+a[i]
     但是这样我们就会发现一个问题:如果是以现在的i为最近公共祖先的两个点呢?是不是没有被包括?
     所以这样的状态不够,我们设计dp[N][V][2]的状态,01表示回不回i节点
     所以得出:dp[i][j][0]=max(dp[i][j][0],dp[i->son][t-1][0]+dp[i][j-t][1]+a[i]);//从其它子树转了一圈回来再转到这可子树并不回去了。
          dp[i][j][0]=max(dp[i][j][0],dp[i->son][t-1][1]+dp[i][j-t][0]+a[i])//从这颗子树回到i再到其它子树不会来了
          dp[i][j][1]=max(dp[i][j][1],dp[i->son][t-1][1]+dp[i][j-t][1]+a[i])//走回i
 
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=100001;
const int INF=999999;
int N,V;
vector<int> vec[301];
int dp[301][301][2];
int a[301]; void dfs(int x,int fa){
for(int i=0;i<vec[x].size();i++){
if(vec[x][i]==fa) continue;
dfs(vec[x][i],x);
int SON=vec[x][i];
for(int v=V;v>=1;v--){
for(int k=1;k<=v;k++){
dp[x][v][0]=max(dp[x][v][0],dp[x][v-k][1]+dp[SON][k-1][0]);//从别的子树兜了一圈到这可子树,不回去了
dp[x][v][0]=max(dp[x][v][0],dp[x][v-k][0]+dp[SON][k-2][1]);//从这颗子树兜了一圈到别的子树,不回去了
dp[x][v][1]=max(dp[x][v][1],dp[x][v-k][1]+dp[SON][k-2][1]);//从别的子树兜了一圈到这可子树然后还回去
}
}
}
}
int ans; int main(){
while(scanf("%d%d",&N,&V)!=EOF){
for(int i=1;i<=300;i++) vec[i].clear();
memset(dp,0,sizeof(dp));
ans=0;
for(int i=1;i<=N;i++) a[i]=read();
if(N==1){//注意要特判
printf("%d\n",a[1]);
continue;
}
for(int i=1;i<N;i++){
int u=read(),v=read();
vec[u].push_back(v);
vec[v].push_back(u);
}
for(int i=1;i<=N;i++)
for(int j=0;j<=V;j++){
dp[i][j][0]=dp[i][j][1]=a[i];
}
dfs(1,0);
printf("%d\n",max(dp[1][V][0],dp[1][V][1]));
}
}

  

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

  1. POJ 2486 树形背包DP Apple Tree

    设d(u, j, 0)表示在以u为根的子树中至多走k步并且最终返回u,能吃到的最多的苹果. 则有状态转移方程: #include <iostream> #include <cstdi ...

  2. HDU 5379 树形DP Mahjong tree

    任意一棵子树上节点的编号连续,每个节点的所有二字节点连续,求编号方案的总数. 稍微分析一下可知 每个节点的非叶子节点个数不能多于两个,否则这个子树无解,从而整棵树都无解. 每棵子树将所有节点按照编号从 ...

  3. CodeForces 109C 树形DP Lucky Tree

    赶脚官方题解写得挺清楚的说,=_= 注意数据范围用long long,否则会溢出. #include <iostream> #include <cstdio> #include ...

  4. 【POJ 2486】 Apple Tree (树形DP)

    Apple Tree Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to a ...

  5. poj 2486 Apple Tree(树形DP 状态方程有点难想)

    Apple Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9808   Accepted: 3260 Descri ...

  6. URAL1018 Binary Apple Tree(树形DP)

    题目大概说一棵n结点二叉苹果树,n-1个分支,每个分支各有苹果,1是根,要删掉若干个分支,保留q个分支,问最多能保留几个苹果. 挺简单的树形DP,因为是二叉树,都不需要树上背包什么的. dp[u][k ...

  7. POJ 2486 Apple Tree(树形DP)

    题目链接 树形DP很弱啊,开始看题,觉得貌似挺简单的,然后发现貌似还可以往回走...然后就不知道怎么做了... 看看了题解http://www.cnblogs.com/wuyiqi/archive/2 ...

  8. ural 1018 Binary Apple Tree(树形dp | 经典)

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  9. Apple Tree POJ - 2486 (树形dp)

    题目链接: D - 树形dp  POJ - 2486 题目大意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走V步,最多能遍历到的权值 学习网址:https://blog.c ...

随机推荐

  1. 【洛谷 P1073】 最优贸易 (Tarjan缩点+拓扑排序)

    题目链接 先\(Tarjan\)缩点,记录每个环内的最大值和最小值. 然后跑拓扑排序,\(Min[u]\)表示到\(u\)的最小值,\(ans[u]\)表示到\(u\)的答案,\(Min\)和\(an ...

  2. 对vue中 默认的 config/index.js:配置的详细理解 -【以及webpack配置的理解】-config配置的目的都是为了服务webpack的配置,给不同的编译条件提供配置

    当我们需要和后台分离部署的时候,必须配置config/index.js: 用vue-cli 自动构建的目录里面  (环境变量及其基本变量的配置) var path = require('path') ...

  3. 【swupdate文档 二】许可证

    许可证 SWUpdate是免费软件.它的版权属于Stefano Babic和其他许多贡献代码的人(详情请参阅实际源代码和git提交信息). 您可以根据自由软件基金会发布的GNU通用公共许可证第2版的条 ...

  4. python基础===进程,线程,协程的区别(转)

    本文转自:http://blog.csdn.net/hairetz/article/details/16119911 进程拥有自己独立的堆和栈,既不共享堆,亦不共享栈,进程由操作系统调度. 线程拥有自 ...

  5. 在LINUX平台上手动创建多个实例(oracle11g)

    在LINUX平台上手动创建多个实例(oracle11g) http://blog.csdn.net/sunchenglu7/article/details/39676659 ORACLE linux ...

  6. Win10下Anaconda3安装CPU版本TensorFlow并使用Pycharm开发

    环境:windows10 软件:Anaconda3 1.安装Anaconda 选择相应的Anaconda进行安装,下载地址点击这里,下载对应系统版本的Anaconda3. 运行 开始菜单->An ...

  7. 辨别苹果数据线真伪 苹果计算器 Dashboard 知识

    辨别苹果数据线真伪 苹果计算器 Dashboard 知识  苹果数据线真伪的最简单的辨别: 线质柔软 用数据线连接适配器(苹果自带的适配器)充电 连接手机 如果该手机数据线是假的, 在手机上会提示”该 ...

  8. JVM对象分配和GC分布【JVM】

    最近在学习java基础结构,刚好学到了jvm,总结了以下并可以结合思维导图认识以下Jvm的对象: 栈:什么是栈? 先说一下栈的数据结构吧,栈它是一种先进后出的数据结构(FILO),跟队列刚好相反(先进 ...

  9. 关于aspxgridview里面过长内容只显示的一部分的处理方案

    protected void g_Message_CustomColumnDisplayText(object sender, ASPxGridViewColumnDisplayTextEventAr ...

  10. 如何设置static tableview的section区域高度

    重写代理方法- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { i ...