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. win10下pip3安装tesserocr时报错

    使用pip3在线安装tesserocr时报错,刚开始报错内容是提示未安装vs2014,安装完以后报错内容如下 ERROR: Command errored out with exit status 1 ...

  2. POJ 2305:Basic remains 进制转换

    Basic remains Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5221   Accepted: 2203 Des ...

  3. WTL之手动编写框架窗口

    新版博客已经搭建好了,有问题请访问 htt://www.crazydebug.com 本人是一个实践主义者,不罗嗦上一篇工程搭建好以后,这一篇就开始写代码,写之前再说几句,如果你熟悉MFC分析过MFC ...

  4. 操作数据库的SqlHelper类

    public static class SqlHelper { public static readonly string connstr = ConfigurationManager.Connect ...

  5. CSS - 强制换行和禁止换行强制换行 和禁止换行样

    强制换行 word-break: break-all;       只对英文起作用,以字母作为换行依据. word-wrap: break-word;   只对英文起作用,以单词作为换行依据. whi ...

  6. spctl命令返回的结果输入到文本中

    说一下我遇到的问题. mac自动打包完之后上传到苹果商店公正,公正后需要检查一下公正的结果, spctl -a -v LBCast.app #查看是否公证成功   显示如下表示公证成功了,有Notar ...

  7. 指令——cd

    一个完整的指令的标准格式: Linux通用的格式——#指令主体(空格) [选项](空格) [操作对象] 一个指令可以包含多个选项,操作对象也可以是多个. 命令:#cd (change director ...

  8. 七十六、SAP中数据库的查询用法之 COUNT(总数),SUM(求和),AVG(求平均),GROUP BY(分组)

    一.我们来查看一个sbook的数据库 二.查看这个表的内容如下 三.表数据如下 四.代码如下 五.结果如下 *&---------------------------------------- ...

  9. [CSS]水平垂直居中方案

    简单总结一下常用的水平垂直居中方案 直接在父级元素设置 text-align 和 line-height ,针对未浮动的行内元素 <div class="box"> & ...

  10. js连续的日期判断,判断相差几天

    var startTime=Date.parse(new Date('2020-02-28')); var endTime=Date.parse(new Date('2020-02-29')); $. ...