POJ2486 Apple Tree

Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu

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:[email protected]

若当前走到了结点x,已经走了y步,除了走向子树外,还有另一选择:返回父节点,去父节点的其他子树。

所以比一般的树状DP多加一维状态,记录有没有返回当前结点。0表示要回,1表示不回。

懒得写分析了,复制一份233

dp[root][j][0] = MAX (dp[root][j][0] , dp[root][j-k][0] + dp[son][k-2][0]);//从s出发,要回到s,需要多走两步s-t,t-s,分配给t子树k步,其他子树j-k步,都返回

dp[root][j]][1] = MAX(  dp[root][j][1] , dp[root][j-k][0] + dp[son][k-1][1]) ;//先遍历s的其他子树,回到s,遍历t子树,在当前子树t不返回,多走一步

dp[root][j][1] = MAX (dp[root][j][1] , dp[root][j-k][1] + dp[son][k-2][0]);//不回到s(去s的其他子树),在t子树返回,同样有多出两步

by键盘上的舞者

我实际写的时候0和1与上面说的相反。

 1 /*by SilverN*/
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 using namespace std;
 8 const int mxn=240;
 9 struct edge{
10     int v;
11     int nxt;
12 }e[mxn];
13 int hd[mxn],mct=0;
14 void add_edge(int u,int v){
15     e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct;
16     return;
17 }
18 int f[mxn][mxn][2];//第三维0表示不返回根节点,1表示返回根节点
19 int w[mxn];
20 int n,m;
21 void dp(int u,int fa){
22     int i,j,k;
23     for(i=hd[u];i;i=e[i].nxt){
24         int v=e[i].v;
25         if(v==fa)continue;
26         dp(v,u);
27         for(j=m;j;--j){
28             for(k=1;k<=j;++k){
29                 f[u][j][1]=max(f[u][j][1],f[u][j-k][1]+f[v][k-2][1]);
30                 f[u][j][0]=max(f[u][j][0],f[u][j-k][1]+f[v][k-1][0]);
31                 f[u][j][0]=max(f[u][j][0],f[u][j-k][0]+f[v][k-2][1]);
32             }
33         }
34     }
35     return;
36 }
37 int main(){
38     while(scanf("%d%d",&n,&m)!=EOF){
39         memset(e,0,sizeof e);
40         memset(hd,0,sizeof hd);
41         memset(f,0,sizeof 0);
42         mct=0;
43         int i,j;
44         for(i=1;i<=n;i++){
45             scanf("%d",&w[i]);
46             for(j=0;j<=m;j++){
47                 f[i][j][0]=f[i][j][1]=w[i];
48             }
49         }
50         int u,v;
51         for(i=1;i<n;i++){
52             scanf("%d%d",&u,&v);
53             add_edge(u,v);
54             add_edge(v,u);
55         }
56         dp(1,0);
57         printf("%d\n",f[1][m][0]);
58     }
59     return 0;
60 }
时间: 2025-01-02 14:08:22

POJ2486 Apple Tree的相关文章

POJ-2486 Apple Tree (树形DP)

题目大意:一棵点带权有根树,根节点为1.从根节点出发,走k步,求能收集的最大权值和. 题目分析:从一个点向其某棵子树出发有三种可能的情况: 1.停留在那棵子树上: 2.再回到这个点: 3.经过这个点走向了其他分支: 定义状态dp(u,k,0/1)表示在u节点为根的子树上走k步并且不回/回到u的最大权值和.则状态转移方程为: dp(u,k,0)=max(dp(son,j-2,1)+dp(u,k-j,0),dp(u,k-j,1)+dp(son,j-1,0)) dp(u,k,1)=max(dp(son

【poj2486】【Apple Tree】【树形dp】

Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7893 Accepted: 2642 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

POJ2486---Apple Tree

Apple Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7598   Accepted: 2548 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 amoun

POJ3321 Apple Tree

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26989   Accepted: 8004 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been

timus 1018. Binary Apple Tree

1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a binary tree, i.e. any biparous branch splits up to exactly two new branches. We will enu

poj 2486 a apple tree

题意:n节点的树,从1开始走,总共v步,每个点都有一个价值,求可以获得的最大价值 分析:这个显然可以走回来,那么就加一维表示是否走回祖先 dp[u][i][j]表示从u为根节点的子树,往下走i步,j=0表示不走回来,j=1表示走回来 那么可以得到状态转移方程,不走回来的可能会影响走回来的,如果先算不会来的,那么一个节点在不走回来算一次 在走回来又算一次,所以先算走回来的就可以避免 dp[u][i][0]=max(dp[u][i][0],dp[u][i-j][1]+dp[v][j-1][0]);

cf202-div 1-B - Apple Tree:搜索,数论,树的遍历

http://codeforces.com/contest/348/problem/B B. Apple Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a rooted tree with n vertices. In each leaf vertex there's a single

ural 1018 Binary Apple Tree

1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a binary tree, i.e. any biparous branch splits up to exactly two new branches. We will enu

2014 Multi-University Training Contest 6 Apple Tree(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 Apple Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 176    Accepted Submission(s): 120 Problem Description I've bought an orchard an