#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为根的树的节点个数. //这里…
CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划) Description 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点)这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1.我们用一根树枝两端连接的结点的编号来描述一根树枝的位置.现在这颗树枝条太多了,需要剪枝.但是一些树枝上长有苹果. 给定需要保留的树枝数量,求出最多能留住多少苹果.下面是一颗有 4 个树枝的树. 2 5 \ / 3 4…
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 enumerate by integers the root of binary apple tree, points of branch…
本文出自   http://blog.csdn.net/shuangde800 --------------------------------------------------------------------------------- 题目链接:  url-1018 题意 给一棵边有权值的二叉树,节点编号为1-n,1是根节点.求砍掉一些边,只保留q条边,这q条边构成的子树    的根节点要求是1,问这颗子树的最大权值是多少? 思路 非常经典的一道树形dp题,根据我目前做过的题来看,有多道…
这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过程中,有点难表示转移 后来看了下大神的做法才知道其实可以用背包来模拟 树枝的去留,其实真的是个背包诶,每个子树枝就相当于物品,他占用了多少树枝量,带来多少的收益,就是用背包嘛,于是用树形DP+背包就可以做了 #include <iostream> #include <cstdio> #…
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1018 Dynamic Programming. 首先要根据input建立树形结构,然后在用DP来寻找最佳结果.dp[i][j]表示node i的子树上最多保存j个分支的最佳结果.代码如下: #include <iostream> #include <math.h> #include <stdio.h> #include <cstdio> #incl…
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…
题目链接:https://vjudge.net/problem/POJ-2486 题意:一棵点权树,起点在1,求最多经过m条边的最大点权和. 思路: 树形dp经典题.用3维状态,dp[u][j][0/1]表示在子树u中走j步的最大价值(回到u/不回到u).显然dp[u][j][1]>=dp[u][j][0],所以dp[1][m][1]就是最终答案. 假设v为u的子结点,k为到v且在v中所用步数,那么转移方程分3步,为: dp[u][j][0]=max(dp[u][j][0] , dp[u][j-…
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…