Preface
\ (DP \) is really profound and profound ......
Introduction
Tree \ (DP \)As the name suggests, it is the \ (DP \) operated on the tree. Generally, \ (f_ I \) can be used to represent the optimal solution in the subtree with the node numbered \ (I \) as the root.
During the transfer, the information is generally transferred from the child node to the Father's Day point, that is, the information is transferred from the bottom up.
Therefore, the tree \ (DP \) usually usesRecursion.
Example 1: Tree backpack
Tree \ (DP \) has a classic question type:Tree backpack.
In fact, its thoughts andOrdinary backpackThe key lies in the time complexity of its metaphysics.
Many seemingly \ (O (N ^ 3) \) will \ (T \) Fly (Actually, it does.), Maybe you can use \ (O (N ^ 3) \) to carry a backpack on the tree (I will not prove the time complexity ),And not because of data.
For more information, see [Luogu 1273.
Example 2: diameter of the weighted tree
The diameter of a common tree can be obtained by \ (BFS \). However, if it is weighted, \ (BFS \) will be cracked (Unfortunately, I did not know before.).
So I used it.Tree \ (DP \).
You can refer to an example: [Miscellaneous] access plan.
Examples
Well, \ (DP \) seems to have nothing to talk about. This is not detailed enough. Let's take a look at the example.
Example 1:[51nod1299] fleeing from prison
This is an interesting tree \ (DP \) question. We can consider using the \ (f \) array to record the status of each node:Completely blocked,This node can be used to reach the leaf node.,A prisoner can reach this node.Then it is not difficult to calculate the answer.
Example 2:[Bzoj4033] [haoi2015] tree Dyeing
Typical tree \ (DP \) questions. The most noteworthy part of this question is not the \ (DP \) process, but the attentionIn a tree with \ (n \) nodes, it is equivalent to dyeing \ (M \) nodes black and \ (n-m \) nodes black.Without this optimization, it will \ (TLE \).
Example 3:[Bzoj1040] [zjoi2008] server guard
A disgustingBase ring outward tree \ (DP \)It should be a comparison template.
Dynamic Planning (II)-tree DP