URAL1018 Binary Apple Tree(树形DP)

题目大概说一棵n结点二叉苹果树,n-1个分支,每个分支各有苹果,1是根,要删掉若干个分支,保留q个分支,问最多能保留几个苹果。

挺简单的树形DP,因为是二叉树,都不需要树上背包什么的。

  • dp[u][k]表示以u结点为根的子树保留k个分支最多能有的苹果数
  • 转移就是左子树若干个,右子树若干个转移。。
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 #define MAXN 111
 6 struct Edge{
 7     int v,w,next;
 8 }edge[MAXN<<1];
 9 int NE,head[MAXN];
10 void addEdge(int u,int v,int w){
11     edge[NE].v=v; edge[NE].w=w; edge[NE].next=head[u];
12     head[u]=NE++;
13 }
14 int n,q,d[MAXN][MAXN];
15 void dp(int u,int fa){
16     d[u][0]=0;
17     int lson=-1,rson=-1,w1,w2;
18     for(int i=head[u]; i!=-1; i=edge[i].next){
19         int v=edge[i].v;
20         if(v==fa) continue;
21         if(lson==-1) lson=v,w1=edge[i].w;
22         else rson=v,w2=edge[i].w;
23         dp(v,u);
24     }
25     if(lson==-1) return;
26     if(rson==-1){
27         for(int i=0; i<q; ++i){
28             if(d[lson][i]==-1) continue;
29             d[u][i+1]=max(d[u][i+1],d[lson][i]+w1);
30         }
31         return;
32     }
33     for(int i=0; i<=q; ++i){
34         if(d[lson][i]==-1) continue;
35         for(int j=0; j<=q-i; ++j){
36             if(d[rson][j]==-1) continue;
37             if(i+j+2<=q) d[u][i+j+2]=max(d[u][i+j+2],d[lson][i]+d[rson][j]+w1+w2);
38             if(i==0 && j+1<=q) d[u][j+1]=max(d[u][j+1],d[rson][j]+w2);
39             if(j==0 && i+1<=q) d[u][i+1]=max(d[u][i+1],d[lson][i]+w1);
40         }
41     }
42 }
43 int main(){
44     int a,b,c;
45     while(~scanf("%d%d",&n,&q)){
46         NE=0;
47         memset(head,-1,sizeof(head));
48         for(int i=1; i<n; ++i){
49             scanf("%d%d%d",&a,&b,&c);
50             addEdge(a,b,c);
51             addEdge(b,a,c);
52         }
53         memset(d,-1,sizeof(d));
54         dp(1,1);
55         printf("%d\n",d[1][q]);
56     }
57     return 0;
58 }
时间: 2024-12-19 19:26:34

URAL1018 Binary Apple Tree(树形DP)的相关文章

URAL_1018 Binary Apple Tree 树形DP+背包

这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过程中,有点难表示转移 后来看了下大神的做法才知道其实可以用背包来模拟 树枝的去留,其实真的是个背包诶,每个子树枝就相当于物品,他占用了多少树枝量,带来多少的收益,就是用背包嘛,于是用树形DP+背包就可以做了 #include <iostream> #include <cstdio> #

URAL 1018 Binary Apple Tree 树形DP 好题 经典

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

URAL1018 Binary Apple Tree

Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description 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 e

【POJ 2486】 Apple Tree (树形DP)

Apple Tree 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 apple

POJ 2486 Apple Tree (树形dp 经典题)

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

POJ 2486 Apple Tree 树形DP+分组背包

链接:http://poj.org/problem?id=2486 题意:一棵(苹果)树,树上有N个结点(N<=100),起点是结点1.每个结点上有若干个苹果,我可以进行K步操作(K<=200),每次操作是从当前结点移动到相邻的结点,并且到了相邻的结点以后会吃掉上面的所有苹果并且苹果不再长出来,相邻是指两个结点之间有边相连.问在K步操作之后最多可以吃掉多少个苹果. 思路:刚入手的时候觉得是一般的树形背包问题,dp[i][j]代表的是以i为根的子树中走j个结点所能吃到的苹果数,来进行状态转移,但

URAL-1018 Binary Apple Tree---树形DP

题目链接: https://cn.vjudge.net/problem/URAL-1018 题目大意: 给你一棵树,每条边有一个边权,求以1为根节点,q条边的子数(q+1个点),边权和至最大. 解题思路: dp[root][j], 表示以root为根节点,保留j个节点的最大边权和. dp[root][j]=max(dp[root][j],dp[root][j-t]+dp[son][t]+len); t的范围从1到j - 1,因为每个点从dp[][1]开始更新 1 #include<bits/st

POJ 2486 Apple Tree (树形dp)

这是一个树上的背包转移.注意要用dp[i][j][k]表示第i个节点用了j的路程是否回到i节点,k=0表示回到i点,k=1表示不回到i点.那么实际上就是树上的一个背包转移. #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <string> #include <cctype> #include

BNUOJ 13358 Binary Apple Tree

Binary Apple Tree Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Original ID: 101864-bit integer IO format: %lld      Java class name: (Any) Let's imagine how apple tree looks in binary computer world. You're right, it