Luogu P2015 二叉苹果树

版权声明:欢迎转载ヽ(•ω•ゞ)···如有表意不清或您没有看懂评论即可! https://blog.csdn.net/yandaoqiusheng/article/details/84942188

题目链接:传送门

题目描述

有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点)
这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1。
我们用一根树枝两端连接的结点的编号来描述一根树枝的位置。下面是一颗有4个树枝的树
在这里插入图片描述
现在这颗树枝条太多了,需要剪枝。但是一些树枝上长有苹果。
给定需要保留的树枝数量,求出最多能留住多少苹果。

输入格式:

第1行2个数,N和Q(1<=Q<= N,1<N<=100)。
N表示树的结点数,Q表示要保留的树枝数量。接下来N-1行描述树枝的信息。
每行3个整数,前两个是它连接的结点的编号。第3个数是这根树枝上苹果的数量。
每根树枝上的苹果不超过30000个。

输出格式:

一个数,最多能留住的苹果的数量。

输入样例

5 2
1 3 1
1 4 10
2 3 20
3 5 20

输出样例

21

首先,题目中说了,这是颗二叉树
有什么用呢?
没什么用
其实这会让题目变得没有那么复杂,一个节点只有左右两个儿子,存图和遍历的时候就多了一种方式
这是树形背包的经典题
然后我们开始设dp数组
f [ i ] [ j ] f[i][j] 表示以 i i 节点为根的子树保留 j j 条边时保留下来的最大苹果数量
为什么这样设呢?
当然是看你的悟性。
如何看转移

f [ f r ] [ j ] = m a x ( f [ f r ] [ j ] , f [ f r ] [ j k 1 ] + f [ c a ] [ k ] + e d g e [ i ] . d i s ) f[fr][j] = max(f[fr][j], f[fr][j - k - 1] + f[ca][k] + edge[i].dis)

当前节点为 f r fr
现在遍历到的节点为 c a ca
在循环中我们枚举子树的边数 k k
总边数为 j j
所以子树的价值为 f [ c a ] [ k ] f[ca][k]
因为还有 f r fr t o to 这条边
所以边数要再减 1 1
剩下的价值就是 f [ f r ] [ j k 1 ] f[fr][j - k - 1]
最后加上这一条边上的苹果就好了
代码中还会有解释

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <vector>
#include <iomanip>
#define A 1000010
#define B 2010
#define ll long long

using namespace std;
struct node {
	int next, to, dis;
}edge[A];
int head[A], num_edge;
void add_edge(int from, int to, int dis) {
	edge[++num_edge].next = head[from];
	edge[num_edge].to = to;
	edge[num_edge].dis = dis;
	head[from] = num_edge;
}
int f[B][B], n, q, a, b, c, size[A];
void dfs(int fr, int fa) {
	for (int i = head[fr]; i; i = edge[i].next) {
		int ca = edge[i].to;
		if (ca == fa) continue;
		dfs(ca, fr);
		size[fr] += size[ca] + 1; //以fr为根的子树有多少条边
		for (int j = size[fr]; j >= 0; j--) //背包容量即为子树的边数
		  for (int k = j - 1; k >= 0; k--) //枚举子树边数
		    f[fr][j] = max(f[fr][j], f[fr][j - k - 1] + f[ca][k] + edge[i].dis);
	}
}
int main() {
	cin >> n >> q;
	for (int i = 1; i < n; i++) {
		cin >> a >> b >> c;
		add_edge(a, b, c);
		add_edge(b, a, c);
	}
	dfs(1, 0);
	cout << f[1][q];
}

猜你喜欢

转载自blog.csdn.net/yandaoqiusheng/article/details/84942188