洛谷P2015 二叉苹果树【树形dp】

P2015 二叉苹果树
时间限制 1.00s
内存限制 125.00MB
题目描述
有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点)

这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1。

我们用一根树枝两端连接的结点的编号来描述一根树枝的位置。下面是一颗有4个树枝的树

2 5
\ /
3 4
\ /
1
现在这颗树枝条太多了,需要剪枝。但是一些树枝上长有苹果。

给定需要保留的树枝数量,求出最多能留住多少苹果。

输入格式
第1行2个数,N和Q(1<=Q<= N,1<N<=100)。

N表示树的结点数,Q表示要保留的树枝数量。接下来N-1行描述树枝的信息。

每行3个整数,前两个是它连接的结点的编号。第3个数是这根树枝上苹果的数量。

每根树枝上的苹果不超过30000个。

输出格式
一个数,最多能留住的苹果的数量。

输入输出样例
输入 #1 复制
5 2
1 3 1
1 4 10
2 3 20
3 5 20
输出 #1 复制
21

解题思路:
dp[u][j]dp[u][j]dp[u][j]:以第uuu节点为根的子树保留jjj所留下的最大苹果数
则:dp[u][j]=max(dp[u][j],dp[u][j−k]+dp[v][k−1]+val)dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k-1]+val)dp[u][j]=max(dp[u][j],dp[u][jk]+dp[v][k1]+val)
因此要先一个循环遍历jjj的值,jjj最多的值为树的边数与需保留的边数的较小值
还需遍历kkk的值,即其子树能保留的边数

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
struct node
{int to;int val;node(int _to,int _val) {to=_to;val=_val;}
};
vector<node> m[120];
int n,k;
int dp[120][120];
int dfs(int s,int f) {int d=0;for(int i=0;i<m[s].size();i++) {if(m[s][i].to==f) continue;d+=dfs(m[s][i].to,s)+1;for(int j=min(k,d);j>0;j--) {for(int t=min(j,d);t>0;t--) {dp[s][j]=max(dp[s][j],dp[m[s][i].to][t-1]+dp[s][j-t]+m[s][i].val);}}}return d;
}
int main() 
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);//ios::sync_with_stdio(0),cin.tie(0);scanf("%d %d",&n,&k);int a,b,c;rep(i,1,n-1) {scanf("%d %d %d",&a,&b,&c);m[a].push_back(node(b,c));m[b].push_back(node(a,c));}dfs(1,0);printf("%d\n",dp[1][k]);return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/536274.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

洛谷P2014【树形dp】

P2014 选课 时间限制 1.00s 内存限制 125.00MB 题目描述 在大学里每个学生&#xff0c;为了达到一定的学分&#xff0c;必须从很多课程里选择一些课程来学习&#xff0c;在课程里有些课程必须在某些课程之前学习&#xff0c;如高等数学总是在其它课程之前学习。现在有N门功课&…

洛谷P2016 战略游戏【树形dp】

P2016 战略游戏 时间限制 1.00s 内存限制 125.00MB 题目描述 Bob喜欢玩电脑游戏&#xff0c;特别是战略游戏。但是他经常无法找到快速玩过游戏的办法。现在他有个问题。 他要建立一个古城堡&#xff0c;城堡中的路形成一棵树。他要在这棵树的结点上放置最少数目的士兵&#x…

Shell Pyramid【数学+二分】

Shell Pyramid 时间限制: 1 Sec 内存限制: 128 MB 提交: 291 解决: 95 [提交] [状态] [命题人:admin] 题目描述 In the 17th century, with thunderous noise, dense smoke and blazing fire, battles on the sea were just the same as those in the modern times. But at tha…

Degree Sequence of Graph G【模拟】

Degree Sequence of Graph G 时间限制: 1 Sec 内存限制: 128 MB 提交: 362 解决: 92 [提交] [状态] [命题人:admin] 题目描述 Wang Haiyang is a strong and optimistic Chinese youngster. Although born and brought up in the northern inland city Harbin, he has deep lov…

Simple Addition expression【打表+二分】

Simple Addition expression 时间限制: 1 Sec 内存限制: 128 MB 提交: 355 解决: 80 [提交] [状态] [命题人:admin] 题目描述 A luxury yacht with 100 passengers on board is sailing on the sea in the twilight. The yacht is ablaze with lights and there comes out laug…

洛谷P2622 关灯问题II【状压dp+bfs】

P2622 关灯问题II 题目描述 现有n盏灯&#xff0c;以及m个按钮。每个按钮可以同时控制这n盏灯——按下了第i个按钮&#xff0c;对于所有的灯都有一个效果。按下i按钮对于第j盏灯&#xff0c;是下面3中效果之一&#xff1a;如果a[i][j]为1&#xff0c;那么当这盏灯开了的时候&am…

洛谷P1879 [USACO06NOV]玉米田Corn Fields【状压dp】

P1879 [USACO06NOV]玉米田Corn Fields 时间限制 1.00s 内存限制 125.00MB 题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number…

LEAGUE TABLES【模拟】

LEAGUE TABLES 时间限制: 1 Sec 内存限制: 128 MB 提交: 349 解决: 150 [提交] [状态] [命题人:admin] 题目描述 League football (known in some circles as soccer) has been played in England since 1888 and is the most popular winter game through most of Europe, jus…

MUSICAL CHAIRS【模拟】

MUSICAL CHAIRS 时间限制: 1 Sec 内存限制: 128 MB 提交: 386 解决: 76 [提交] [状态] [命题人:admin] 题目描述 Musical chairs is a game frequently played at children’s parties. Players are seated in a circle facing outwards. When the music starts, the players h…

Bomb HDU - 3555【数位dp】

Bomb HDU - 3555 The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the time bomb. The number sequence of the time bomb counts from 1 to N. If the current number sequence includes the sub-sequence “49”, the power…

不要62 HDU - 2089【数位dp】

不要62 HDU - 2089 杭州人称那些傻乎乎粘嗒嗒的人为62&#xff08;音&#xff1a;laoer&#xff09;。 杭州交通管理局经常会扩充一些的士车牌照&#xff0c;新近出来一个好消息&#xff0c;以后上牌照&#xff0c;不再含有不吉利的数字了&#xff0c;这样一来&#xff0c;就可…

PACKING【二维01背包】

PACKING 时间限制: 1 Sec 内存限制: 128 MB 提交: 278 解决: 24 [提交] [状态] [命题人:admin] 题目描述 It was bound to happen. Modernisation has reached the North Pole. Faced with escalating costs for feeding Santa Claus and the reindeer, and serious difficulti…

机器人军团【动态规划】

机器人军团 时间限制: 1 Sec 内存限制: 64 MB 提交: 279 解决: 139 [提交] [状态] [命题人:admin] 题目描述 邪狼&#xff1a;“怎么感觉这些机器人比我还聪明&#xff1f;不是说人工智能永远不能超越人类吗&#xff1f;” 天顶星人&#xff1a;“你们真是目光短浅&#xff0c…

【动态规划】抄近路

【动态规划】抄近路 时间限制: 1 Sec 内存限制: 64 MB 提交: 105 解决: 68 [提交] [状态] [命题人:admin] 题目描述 “最近不知道怎么回事&#xff0c;感觉我们这个城市变成了一个神奇的地方&#xff0c;有时在路上走着走着人就消失了&#xff01;走着走着突然又有人出现了&…

【动态规划】魔法石矿

【动态规划】魔法石矿 时间限制: 1 Sec 内存限制: 64 MB 提交: 116 解决: 27 [提交] [状态] [命题人:admin] 题目描述 为了找到回家的路&#xff0c;张琪曼施展魔法&#xff0c;从高维空间召唤出了一种叫作“读者”的生物&#xff0c;据说“读者”这种生物无所不能&#xff0c;…

Knapsack Cryptosystem【折半+查找】

链接&#xff1a;https://ac.nowcoder.com/acm/contest/889/D 来源&#xff1a;牛客网 Amy asks Mr. B problem D. Please help Mr. B to solve the following problem. Amy wants to crack Merkle–Hellman knapsack cryptosystem. Please help it. Given an array {ai} wi…

All men are brothers【并查集+数学】

链接&#xff1a;https://ac.nowcoder.com/acm/contest/889/E 来源&#xff1a;牛客网 题目描述 Amy asks Mr. B problem E. Please help Mr. B to solve the following problem. There are n people, who don’t know each other at the beginning. There are m turns. In e…

Light bulbs【差分】

19.98% 1000ms 8192K There are NN light bulbs indexed from 00 to N-1N−1. Initially, all of them are off. A FLIP operation switches the state of a contiguous subset of bulbs. FLIP(L, R)FLIP(L,R)means to flip all bulbs xx such that L \leq x \leq RL≤x≤R. S…

Digit sum【暴力+打表】

Digit sum 33.57% 2000ms 131072K A digit sum S_b(n)S b ​ (n) is a sum of the base-bb digits of nn. Such as S_{10}(233) 2 3 3 8S 10 ​ (233)2338, S_{2}(8)1 0 0 1S 2 ​ (8)1001, S_{2}(7)1 1 1 3S 2 ​ (7)1113. Given NN and bb, you need to calcu…

P1040 加分二叉树【dp+深搜】

题目描述 设一个nn个节点的二叉树tree的中序遍历为&#xff08;1,2,3,…,n1,2,3,…,n&#xff09;&#xff0c;其中数字1,2,3,…,n1,2,3,…,n为节点编号。每个节点都有一个分数&#xff08;均为正整数&#xff09;&#xff0c;记第ii个节点的分数为di,treedi,tree及它的每个子树…