E - Apple Tree POJ - 2486

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
题意:给你一棵以1为根节点的树,树上的每个节点有arr[i]苹果,从1出发最多能走k步,问最多能得到多少个苹果。
题解:一开始没有思考到能还能走回来的的情况,就以为是一道广搜就可以了,然后听别人说才知道是一个树形dp
起点已经确定为1,那么取得最大值仅有两种情况,一种是走了k步之后,回到1了,另一种是走了k步,终点没回到1,停在某一个子节点上。
那么对于每一个节点的最大值都可以这样认为,每个节点的最大值都是走K步,回到起点/不回到起点。
定义三位数组dp[i][j][k] , i 为起点 , j 为走的步数 , k = 0 表示不回到起点 ,k = 1 表示回到起点。
每个父亲节点的值,都可以由他的子节点来更新
对于状态转移方程
dp[i][j][1] = max(dp[i][j][1] , dp[i][j - m][1] + dp[v][m - 2][1]);
最终都要返回起点i,dp[v][m - 2][1] 代表从i的其中一个子节点v传递上拉来的走 m - 2步的获取苹果的最大值,之所以是m-2步,因为 i 和 v 之间的往返消耗了两步
dp[i][j][0] = max(dp[i][j][0] , max(dp[i][j - m][1] + dp[v][m - 2][0] , dp[i][j - m][0] + dp[v][m - 2][1]));
最终不返回起点i ,其终点有可能停留在子节点v所在的子树中,也有可能从v节点的子树中返回,停留在另一个子树中。
 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<vector>
#include<queue>
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid + 1,r
#define P pair<ll,ll>
#define ull unsigned long long
using namespace std;
typedef long long ll;
const int maxn = 1e5 + ;
const ll mod = 1e9 + ;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
int k, n, T, m, t;
vector<int>edge[];
int arr[];
int dp[][][]; // 三维 1 表示返回出发点 , 0 表示不返回出发点
void dfs(int u, int start)
{
for (int i = ; i < edge[u].size(); ++i)
{
int v = edge[u][i];
if (v == start) continue;
dfs(v, u);
for (int j = k; j >= ; --j)
{
for (int m = ; m <= j; ++m)
{
if(m == )
dp[u][j][] = max(dp[u][j][], dp[u][j - m][] + dp[v][m - ][]);
//从起点u出发走j步,不返回u的最大值,
else
{
dp[u][j][] = max(dp[u][j][], max(dp[u][j - m][] + dp[v][m - ][] , dp[u][j - m][] + dp[v][m - ][]));
dp[u][j][] = max(dp[u][j][], dp[u][j - m][] + dp[v][m - ][]);
}
}
} }
} int main()
{
while (scanf("%d %d", &n, &k) != EOF)
{
for (int i = ; i <= n; ++i)
edge[i].clear();
mem(dp, );
mem(arr, );
for (int i = ; i <= n; ++i)
scanf("%d", &arr[i]);
for (int i = ; i <= n; ++i)
for (int j = ; j <= k; ++j)
dp[i][j][] = dp[i][j][] = arr[i];
for (int i = ; i < n; ++i)
{
int u, v;
scanf("%d %d", &u, &v);
edge[u].push_back(v);
edge[v].push_back(u);
}
dfs(, -);//建立一个根节点
printf("%d\n", max(dp[][k][], dp[][k][])); }
return ;
}

AC代码

一个从很久以前就开始做的梦。

												

E - Apple Tree POJ - 2486的更多相关文章

  1. Apple Tree POJ - 2486

    Apple Tree POJ - 2486 题目大意:一棵点带权有根树,根节点为1.从根节点出发,走k步,求能收集的最大权值和. 树形dp.复杂度可能是O(玄学),不会超过$O(nk^2)$.(反正这 ...

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

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

  3. Apple Tree POJ - 3321 dfs序列构造树状数组(好题)

    There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...

  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)

    [POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Acce ...

  6. POJ 2486 Apple Tree

    好抽象的树形DP......... Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6411 Accepte ...

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

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

  8. poj 2408 Apple Tree

    http://poj.org/problem?id=2486 典型的回溯题目:特别是状态方程用三维的来标记是否要走回路. 题意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走 ...

  9. POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)

    id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...

随机推荐

  1. flower——知识总结

    创建主外键关联的话,外键表的外键字段一定要与主键表的主键字段相一致,包括字段类型,字段长度,字段符号等等 inverse="true" 将控制权交给对方,在一对多的关系中,一端控制 ...

  2. 前端学习(22)~css问题讲解

    你是如何理解 HTML 语义化的? 语义化:指对文本内容的结构化(内容语义化),选择合乎语义的标签(代码语义化). 举例:段落用 p,边栏用 aside,主要内容用 main 标签. 好处: 便于开发 ...

  3. EditText标签的使用

    前文: 介绍EditText的使用,这个是文本输入控件,用来输入文本内容 使用: EditText继承TextView所以TextView的东西EditText都可以使用 text:显示文本 text ...

  4. Mac 配置代码高亮 Git状态显示

    Mac 一个为开发者量身定做的笔记本,分享给大家希望能帮助大家配置一个好的开发环境,好的开发环境才有好的心情Code. 首先进入到Home到目录一般默认打开的都是Home,如果不是输入 cd ~ 回车 ...

  5. SSM文件上传要点总结

    文件的上传要点: 1.表单方面:enctype="multitype/form-data" 编码方式选择混编码 input 类型采用file 2.实体类一定要进行序列化,也就是im ...

  6. django 中从外界借助多个网站时 static 的存放和整理

    在 模板之家中  前端页面直接上去抓取  可是遇到重复  或者 版本不统一  所以 在每个app下面建立自己的 stastic 在制作的html  页面上方 导入静态页面 {% load static ...

  7. 070-PHP数组相加

    <?php $arr1=array('a','b','c'); //定义一个数组 echo '数组$arr1的信息:<br />'; print_r($arr1); //输出数组信息 ...

  8. HDU_4965 Fast Matrix Calculation 2014多校9 矩阵快速幂+机智的矩阵结合律

    一开始看这个题目以为是个裸的矩阵快速幂的题目, 后来发现会超时,超就超在  M = C^(N*N). 这个操作,而C本身是个N*N的矩阵,N最大为1000. 但是这里有个巧妙的地方就是 C的来源其实 ...

  9. 第十篇 Form表单

    Form表单 阅读目录(Content) Form介绍 普通的登录 使用form组件 Form那些事儿 常用字段演示 校验 使用Django Form流程 补充进阶 应用Bootstrap样式 批量添 ...

  10. Docker 搭建开源跳板机_jumpserver (运维开源堡垒机_jumpserver) Centos_7.0

    最近看到一个开源项目(jumpserver)  很不错  还是用Docker  部署得 ... 抽了点时间拿来学习一下  部署    分析   简单使用一下  ....好了先搭起来 准备 工作:    ...