【POJ 2486】 Apple Tree(树型dp)

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8981   Accepted: 2990

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

难得1A 不枉这两天跟树dp天天见。

题目大意:一棵树。

。。

还是一棵树……每一个节点有一个点权。从树根出发,最远走k步,每走到一个新点。能够把该点的权值累加进去,问最多能够走出多大的权值和。

比較裸的树dp了。dp[i][j][0/1]表示根节点i開始走j步能够走出的最大权值和。近期做树型dp蛮多。发现大多都是这样。要开两个三维空间。一个表示答案。一个是用来辅助求解的。

此题相同,0表示走j步不回到根。1表示终于回到根。

这样发现每一个u来说。新遍历到一个孩子v,

dp[u][i][1]的更新为dp[u][i-j-2][1]+dp[v][j][1] 也就是说加上u到v这一步和从v遍历完返回u的v到u这步还有从v出发j步回到v 组成i

dp[u][i][0]的更新有两种,一种是到v后往后走完不回根 dp[u][i][0] = dp[u][i-j-1][1]+dp[v][j][0]

一种是v往后走完回根。然后从之前某个节点走完不回根 dp[u][i][0] = dp[u][i-j-2][0]+dp[v][j][1]

当然 上面这些都要取最大

代码例如以下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout) using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8; bool mp[111][111];
//0不回 1回
int dp[111][222][2];
int n,k;
int val[111]; void dfs(int u,int pre)
{
dp[u][0][0] = dp[u][0][1] = val[u];
for(int v = 1; v <= n; ++v)
{
if(v == pre || !mp[u][v]) continue;
dfs(v,u);
for(int i = k; i > 0; --i)
{
for(int j = 0; j < i; ++j)
{
if(j+2 <= i)
{
dp[u][i][1] = max(dp[u][i][1],dp[u][i-j-2][1]+dp[v][j][1]);
dp[u][i][0] = max(dp[u][i][0],dp[u][i-j-2][0]+dp[v][j][1]);
}
dp[u][i][0] = max(dp[u][i][0],dp[u][i-j-1][1]+dp[v][j][0]);
}
}
}
} int main()
{
//fread();
//fwrite(); int u,v;
while(~scanf("%d%d",&n,&k))
{
memset(mp,0,sizeof(mp));
for(int i = 1; i <= n; ++i)
scanf("%d",&val[i]);
for(int i = 1; i < n; ++i)
{
scanf("%d%d",&u,&v);
mp[u][v] = mp[v][u] = 1;
}
memset(dp,0,sizeof(dp));
dfs(1,1);
int id = k;
while(id && !dp[1][id][0]) --id;
printf("%d\n",dp[1][id][0]);
} return 0;
}



【POJ 2486】 Apple Tree(树型dp)的更多相关文章

  1. POJ 2486 Apple Tree ( 树型DP )

    #include <iostream> #include <cstring> #include <deque> using namespace std; #defi ...

  2. POJ 2486 Apple Tree [树状DP]

    题目:一棵树,每个结点上都有一些苹果,且相邻两个结点间的距离为1.一个人从根节点(编号为1)开始走,一共可以走k步,问最多可以吃多少苹果. 思路:这里给出数组的定义: dp[0][x][j] 为从结点 ...

  3. POJ 2486 Apple Tree(树形dp)

    http://poj.org/problem?id=2486 题意: 有n个点,每个点有一个权值,从1出发,走k步,最多能获得多少权值.(每个点只能获得一次) 思路: 从1点开始,往下dfs,对于每个 ...

  4. POJ 2486 Apple Tree (树形DP,树形背包)

    题意:给定一棵树图,一个人从点s出发,只能走K步,每个点都有一定数量的苹果,要求收集尽量多的苹果,输出最多苹果数. 思路: 既然是树,而且有限制k步,那么树形DP正好. 考虑1个点的情况:(1)可能在 ...

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

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

  6. POJ 2486 Apple Tree

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

  7. POJ 3321 Apple Tree(树状数组)

                                                              Apple Tree Time Limit: 2000MS   Memory Lim ...

  8. POJ 1947 - Rebuilding Roads 树型DP(泛化背包转移)..

    dp[x][y]表示以x为根的子树要变成有y个点..最少需要减去的边树... 最终ans=max(dp[i][P]+t)  < i=(1,n) , t = i是否为整棵树的根 > 更新的时 ...

  9. POJ 2486 Apple Tree ——(树型DP)

    题意是给出一棵树,每个点都有一个权值,从1开始,最多走k步,问能够经过的所有的点的权值和最大是多少(每个点的权值只能被累加一次). 考虑到一个点可以经过多次,设dp状态为dp[i][j][k],i表示 ...

  10. POJ 2486 Apple Tree(树形DP)

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

随机推荐

  1. selenium3 + Python - 处理浏览器弹窗(转载)

    作者:Real_Tino 转载链接:https://blog.csdn.net/real_tino/article/details/59068827 我们在浏览网页时经常会碰到各种花样的弹窗,在做UI ...

  2. DotNetCasClient加载失败问题分析

    最近公司在接入整理单点登录方案的时候,选择了CAS方案,实际版本采用了4.0.当我们把服务端附属完毕,基于.NET平台Web版的客户端DotNetCasClient进行定制化修改后,在测试环境通过.然 ...

  3. Win7的虚拟Wi-Fi

    前几天无意中发现,Win7的硬件驱动里有个叫Microsoft Virtual WiFi Miniport Adapter的东东,从网上查了一下,可以用来组建临时网络,共享Internet.一块无线网 ...

  4. SQL之LEFT JOIN,EIGHT JOIN,INSERT JOIN的区别

    left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...

  5. C99C新增内容

    继上一篇复合文字之后,今天我们继续谈一谈C99C的新特性. C99标准是继C89标准之后的第二个C语言官方标准,于1999年12月1日正式发布,其中对数据类型(增加了对_Bool),关键字(增加了in ...

  6. Python启动浏览器Firefox\Chrome\IE

    # -*- coding:utf-8 -*- import os import selenium from selenium import webdriver from selenium.webdri ...

  7. MacOS 升级后pod 出现的问题

    -bash: /usr/local/bin/pod: /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby: bad ...

  8. CNN结构:SPP-Net为CNNs添加空间尺度卷积-神经元层

    前几个CNN检测的框架要求网络的图像输入为固定长宽,而SPP-Net在CNN结构中添加了一个实现图像金字塔功能的卷积层SPP层,用于在网络中实现多尺度卷积,由此对应多尺度输入,以此应对图像的缩放变换和 ...

  9. [Advanced Algorithm] - Inventory Update

    题目 依照一个存着新进货物的二维数组,更新存着现有库存(在 arr1 中)的二维数组. 如果货物已存在则更新数量 . 如果没有对应货物则把其加入到数组中,更新最新的数量. 返回当前的库存数组,且按货物 ...

  10. Appium Desired Capabilities信息配置

    编写APPium脚本,必须要配置Desired Capabilities信息 Desired Capabilities 在启动 session 的时候是必须提供的. Desired Capabilit ...