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

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> using namespace std; ; int dp[maxn][maxn]; //dp[i][j]表示以i为根,保留j个点的最大权值. int N,Q; int G[maxn][maxn]; int num[maxn]; //以i为根的树的节点个数. //这里…
这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过程中,有点难表示转移 后来看了下大神的做法才知道其实可以用背包来模拟 树枝的去留,其实真的是个背包诶,每个子树枝就相当于物品,他占用了多少树枝量,带来多少的收益,就是用背包嘛,于是用树形DP+背包就可以做了 #include <iostream> #include <cstdio> #…
组队赛的时候的一道题,那个时候想了一下感觉dp不怎么好写呀,现在写了出来,交上去过了,但是我觉得我还是应该WA的呀,因为总感觉dp的不对. #pragma warning(disable:4996) #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<string> #include<vector> #define max…
题目大概说一棵n结点二叉苹果树,n-1个分支,每个分支各有苹果,1是根,要删掉若干个分支,保留q个分支,问最多能保留几个苹果. 挺简单的树形DP,因为是二叉树,都不需要树上背包什么的. dp[u][k]表示以u结点为根的子树保留k个分支最多能有的苹果数 转移就是左子树若干个,右子树若干个转移.. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define MAXN…
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…
Apple Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9808   Accepted: 3260 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…
题目链接 树形DP很弱啊,开始看题,觉得貌似挺简单的,然后发现貌似还可以往回走...然后就不知道怎么做了... 看看了题解http://www.cnblogs.com/wuyiqi/archive/2012/01/09/2316758.html画画题解中的三种情况,还是可以理解的. 设dp[0][s][j]表示从s(当前根节点)出发,走 j 步,回到s所能获得的最大权值 dp[1][s][j]表示从s(当前根节点)出发,走j步,不回到s所能获得的最大权值 现在我们就可以分配背包容量了:父节点与子…
链接 简单树形DP #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<stdlib.h> using namespace std; #define INF 0xfffffff ][]; ][]; ][]; int dfs(int pre,int u,int s) { int i; ) return dp[u][s]; ,cc[];…
题目链接: 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]开始更新 #include<bits/stdc…
题目大意 给定一棵n个结点的树,每个结点上有一定数量的苹果,你可以从结点1开始走k步(从某个结点走到相邻的结点算一步),经过的结点上的苹果都可以吃掉,问你最多能够吃到多少苹果? 题解 蛋疼的问题就是可以往回走~~~~想N就木有想到解法,看了下网上的解题报告~~~~想到了其实还是挺容易理解的~~~分为两种情况,就是有些点只需要走一次,而有些则需要走两次. 方程表示: dp[0][u][j]表示从结点u开始走j步并且返回到结点u获得的最大价值 dp[1][u][j]表示从结点u开始走j步不回到结点u…