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/stdc++.h>
 2 using namespace std;
 3 const int maxn = 100 + 10;
 4 typedef long long ll;
 5 struct node
 6 {
 7     int v, w;
 8     node(){}
 9     node(int v, int w):v(v), w(w){}
10 };
11 vector<node>Map[maxn];
12 int num[maxn];//num[i]表示以i节点为root的子树中的点的数目
13 int dp[maxn][maxn];//dp[i][j]表示以i节点为root的子树中只有j条边最大权值
14 void dfs(int root, int fa)
15 {
16     num[root] = 1;
17     for(int i = 0; i < Map[root].size(); i++)
18     {
19         int v = Map[root][i].v, w = Map[root][i].w;
20         if(v == fa)continue;//不可回溯
21         dfs(v, root);//先将儿子信息更新好
22         num[root] += num[v];//root子树中当前的节点数目
23         for(int j = num[root]; j >= 1; j--)//更新父节点的dp
24         {
25             for(int k = 1; k < j && k <= num[v]; k++)//k不能等于j,k=j时说明root的点数目为0
26                 dp[root][j] = max(dp[root][j], dp[root][j - k] + dp[v][k] + w);
27         }
28     }
29 }
30 int main()
31 {
32     int n, k;
33     while(scanf("%d%d", &n, &k) != EOF)
34     {
35         memset(dp, 0, sizeof(dp));
36         for(int i = 1; i < n; i++)
37         {
38             int u, v, w;
39             scanf("%d%d%d", &u, &v, &w);
40             Map[u].push_back(node(v, w));
41             Map[v].push_back(node(u, w));
42         }
43         dfs(1, -1);
44         cout<<dp[1][k + 1]<<endl;//包含k条边,也就是k+1个点
45     }
46     return 0;
47 }

原文地址:https://www.cnblogs.com/fzl194/p/9323198.html

时间: 2025-01-04 10:13:00

URAL-1018 Binary Apple Tree---树形DP的相关文章

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

URAL_1018 Binary Apple Tree 树形DP+背包

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

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

Ural 1018 binary apple tree(显性树的树dp)

题意:一棵含n个节点的树,保留m条边,使含m条边的子树的边权和最大: 思路:树dp.求含m+1个节点边权和最大的子树.对每个分支节点有三种操作:剪去左子树:剪去右子树:将其节点数合理分配给左右子树: 记以x为根,含k个节点的子树的最大边权和为g[x][k]. 若x为叶节点,则g[x][k]为x通往父节点的边权:若非叶节点,枚举k-1个节点分配在左右子树的所有可能方案,找到最佳方案: #include<cstdio> #include<cstring> #include<alg

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】 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个结点所能吃到的苹果数,来进行状态转移,但

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