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. 反射①:如何获取class对象六种方法

    获取class对象的六种方法 了解:class类——是Java反射机制的入口,封装了一个类或接口的运行信息,通过调用Class类的方法可以获取这些信息,其特点如下: 1.该类在java.lang包中: ...

  2. 吴裕雄--天生自然java开发常用类库学习笔记:ListIterator接口

    import java.util.ArrayList ; import java.util.List ; import java.util.ListIterator ; public class Li ...

  3. Codeforces Round #618 (Div. 2) 小号上紫之路

    这一场涨了不少,题也比较偏思维,正好适合我 A. Non-zero 我们记录这些数字的总和sum,并且记录0的个数zero,显然答案应该是这些0的个数,注意如果sum+zero==0的话答案要额外加一 ...

  4. UVA - 11354 Bond(最小生成树+LCA+瓶颈路)

    题意:N个点,M条路,每条路的危险度为路上各段中最大的危险度.多组询问,点s到点t的所有路径中最小的危险度. 分析: 1.首先建个最小生成树,则s到t的路径一定是危险度最小的. 原因:建最小生成树的最 ...

  5. 采用idea创建springboot mybatis web工程

    idea是一款强大的利器,最近公司换成采用springboot在eclipse上开发,因为idea的强大,故而,学习一下该利器,搭建springboot的web工程,在下面练习使用. 一.创建spri ...

  6. python 首先生成包含1000个随机字符的字符串,然后统计每个字符的出现次数

    题目:首先生成包含1000个随机字符的字符串,然后统计每个字符的出现次数 import string import random x = string.ascii_letters + string.d ...

  7. Nginx+uwsgi+django部署项目

    nginx把请求转发给uwsgi,然后把uwsgi处理得到的结果返回给浏览器. 安装nginx: yum -y install gcc pcre-devel openssl-devel #安装Ngin ...

  8. Swift 枚举enum

    enum methodType{ case get case post case put case delete } 枚举赋值 enum methodType:String{ case get=&qu ...

  9. Sklearn 预处理数据

    ## 版权所有,转帖注明出处 章节 SciKit-Learn 加载数据集 SciKit-Learn 数据集基本信息 SciKit-Learn 使用matplotlib可视化数据 SciKit-Lear ...

  10. NumPy 基于数值区间创建数组

    来源:Python Numpy 教程 章节 Numpy 介绍 Numpy 安装 NumPy ndarray NumPy 数据类型 NumPy 数组创建 NumPy 基于已有数据创建数组 NumPy 基 ...