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. 五十四、SAP中LVC表格每列的宽度自适应

    一.之前我们的LVC表格输出的界面,有些列太宽余留空白区块太多,有些列则显示不全还带省略号等 二.我们来到'REUSE_ALV_GRID_DISPLAY_LVC'的模块中,查看他的属性 三.我们查看L ...

  2. 141-PHP类的抽象方法和继承实例(一)

    <?php abstract class ren{ //定义人类 //定义成员属性 protected $name=''; protected $age=0; //定义成员方法 public f ...

  3. 071-PHP数组合并

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

  4. vue左侧菜单的实现

    后端实现 django视图def menu(request): menu_list = models.Menu.objects.all().values('id', 'menu_name', 'par ...

  5. UVA - 1149 Bin Packing(装箱)(贪心)

    题意:给定N(N<=10^5)个物品的重量Li,背包的容量M,同时要求每个背包最多装两个物品.求至少要多少个背包才能装下所有的物品. 分析:先排序,从最重的开始装,如果重量小于M,则如果能装一个 ...

  6. css实现下箭头

    css实现下箭头 .top { width:; height:; border-left: 10px solid transparent; border-right: 10px solid trans ...

  7. Dynamics CRM - 解决无法使用 Ribbon Workbench 2016 定制 Sub-Grid View Button 的问题(SubGrid MainTab 消失之谜)

    发现问题: 在 Dynamics CRM 开发中,会经常使用 Ribbon Workbench 工具来定制 Button 或者对已有 Button 进行自定义功能开发,比如隐藏 SubGrid 的 A ...

  8. BZOJ:2243: [SDOI2011]染色

    题解: 树剖,线段树维护区间颜色段数 记录两端点的颜色,做到O(1)合并 问题: 非递归建树实现 #include<iostream> #include<cstdio> #in ...

  9. EL&JSTL简单介绍

    EL表达式 是为了简化咱们的jsp代码,具体一点就是为了简化在jsp里面写的那些java代码. 写法格式 ${表达式 } 如果从作用域中取值,会先从小的作用域开始取,如果没有,就往下一个作用域取. 一 ...

  10. css笔记01

    CSS样式(Cascading Style Sheets) 表格布局缺陷: ​ 嵌套太多,一旦顺序错乱页面达不到预期效果 ​ 表格布局页面不灵活,动一块整个布局全都要变 语法: ​ 在style标签中 ...