DP_树形dp
文章平均质量分 87
sdau_blue
念念不忘,必有回响。驰而不息,功不唐捐。
展开
-
树的直径证明
树的直径求法1: 任意选择一个点M,dfs找到最远距离P,再从P为起点dfs最长路径,端点为Q,此时PQ即为该树的直径。 证明:为什么PQ为直径。即证:P肯定为直径的端点。 以下讨论了三种情况: ...原创 2020-03-17 10:52:24 · 358 阅读 · 3 评论 -
Codeforces Round #525 (Div. 2)—E. Ehab and a component choosing problem(树形dp)
E. Ehab and a component choosing problem time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You're given a tree consisting of n node...原创 2018-12-14 23:17:18 · 143 阅读 · 0 评论 -
D. The Fair Nut and the Best Path(树形dp)
D. The Fair Nut and the Best Path http://codeforces.com/contest/1084/problem/D time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output T...原创 2018-12-12 23:02:28 · 840 阅读 · 0 评论 -
蓝魔法师(树形dp)
链接:https://www.nowcoder.com/acm/contest/215/C 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 “你,你认错人了。我真的,真的不是食人魔。”--蓝魔法师 给出一棵树,求有多少种删边方案,使得删后的图每个连通块大小小...原创 2018-10-27 16:50:31 · 553 阅读 · 0 评论 -
hdu 4003 Find Metal Mineral (树形dp+分组背包)
这道题有两种做法,但是都是树形dp+分组背包 思路1:逆向考虑。 参考:https://blog.csdn.net/shuangde800/article/details/10217167 #include<bits/stdc++.h> using namespace std; const int maxn=10010; int sum,cnt; int dp[maxn][20...原创 2018-08-23 15:21:12 · 259 阅读 · 0 评论 -
Balancing act树形dp(求树的重心)
Balancing ActTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 10726 Accepted: 4463DescriptionConsider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the...原创 2018-04-26 11:04:34 · 278 阅读 · 0 评论 -
#1676 : 树上的等差数列(树形dp)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一棵包含N个节点的无根树,节点编号1~N。其中每个节点都具有一个权值,第i个节点的权值是Ai。 小Hi希望你能找到树上的一条最长路径,满足沿着路径经过的节点的权值序列恰好构成等差数列。 输入 第一行包含一个整数N。 第二行包含N个整数A1, A2, ... AN。 以下N-1行,每行包含...原创 2018-03-21 21:28:15 · 538 阅读 · 2 评论 -
S - Balancing Act
S - Balancing Act Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the balance of a no原创 2017-11-19 17:15:01 · 384 阅读 · 0 评论 -
H - Rebuilding Roads
就是状态的寻找不易想到。题目大意:给你一颗树,对树进行裁剪,问得到p个节点最少裁减多少次 题目分析: 用dp[i][j]表示以i为根结点,得到j个节点最少裁剪的次数 由于每个节点的当前子树是否裁剪是不确定的,因此就有两种情况: 选k为r的孩子之一 (1)裁剪当前子树k:则dp【r】【p】+1 (2)不裁剪当前子树k:则dp【r】【p】=dp【r】【i】+dp【k】【p-i】(0<...原创 2017-11-22 20:14:08 · 254 阅读 · 0 评论 -
树形dp小结——2
树形dp的题一般都结合着背包来用。以下的几道题都是结合着背包的思想来的 1:树形dp+分组背包 状态比较难想 之前说过在最长距离的那道题里说过,不会返回。但是有的提示需要考虑返回的节点的。 下面就是一个例子: Apple Tree 题目大意: 给你一个苹果树,有N个节点,每个节点上都有一个一个苹果也就有一个权值,当你经过这个点将得到权值,重复走节点只能算一次,给N-...原创 2017-11-26 18:32:13 · 201 阅读 · 0 评论 -
树形dp小结——1
1.通常的动态规划都是线性的,或者说是在有向无环图上进行的。而树形dp就是在树的基础上进行的dp 2.树形dp通常有两种方向,一种是自下而上,另一种是自上而下。具体利用方法根据实际要求来 叶——根,在回溯的时候从叶子节点往上更新信息 根——叶,往往是在叶——根dfs一遍以后(相当于预处理),再重新往下获取,得到的最终答案。 3.和普通dp一样,树形dp的重点在于找状态转移方程。原创 2017-11-26 17:21:25 · 264 阅读 · 0 评论