Tree, a very beautiful data structure, because it has a progressive nature, so it and the subtree can pass a lot of information to each other, because it can be used as a restricted graph to perform more operations on the above, various trees are used in different places, such as binary tree, triple tree, static Search Tree, and AVL Tree, line Segment tree, splay tree, suffix tree, Etc ..
There are so many types of data structures that I just want to talk about in the tree. This album only targets dynamic planning on the tree, that is, tree-like DP. the general step of tree-based DP is to first convert the tree to a root tree, then perform a deep search on the tree, and update the information returned from the child node or the child tree to the root node layer by layer. The key here is the returned information. There is no general description, because each question requires different things.
For the moment, the three main parts of this album may not be very good, and the subsequent questions may be changed if they are understood more deeply, but they are all just a few things.
I. general tree DP
1. HDU 1520 anniversary party each node has the right value. The subnode and parent node cannot be selected at the same time. How much value can be selected at the end? Solution report: 2. Typical questions of HDU 2196 computer: Find the shortest distance from each tree point to another point, convert it to a root tree, and search twice in depth. Record the shortest distance from the leaf at a time, update the final answer once. Solution Report: here 3. poj 1741 tree (difficult) typical question: calculate the number of solutions with a small distance of K between two points on the tree, and divide the tree. Solution Report: Here 4, poj 2152 fire (difficult) rare O (N ^ 2) tree-like DP, build a fire station on the tree, each node is required to be less than K distance from the nearest fire station, minimum cost. Solution Report: Here 5, poj 3162 Walking Race (difficult) tree DP to find the maximum distance + the maximum and minimum values of the Line Segment tree query, and then maintain two pointers to traverse the entire sequence. Solution report here
6. Cf 218d. choosing capital for treeland converts the edge direction into edge weight. The forward direction is 0, and the reverse direction is 1. after conversion, the problem is to find that a vertex is the sum of edge weights from the root to all vertices, and those vertices with the smallest sum of edge weights.
Ii. tree-shaped backpack (grouping and processing on trees)
1. poj 1155 tele regards each node's sub-nodes as a group of backpacks. The maximum capacity is the number of leaf child nodes. The selected capacity is the selected capacity, value is the money-transfer fee you give. The solution report here 2, HDU 1011 Starship Troopers is similar to the above question. To select a parent node, you must first select a sub-node and specify M as 0. 3. poj 1947 rebuilding roads calculates to delete at least a few sides so that the number of subtree nodes is P. The specific model is similar to the previous question. Solution Report: 4. HDU 1561 the more. The better selects several vertices on a tree to obtain the maximum value. to select a child node, you must first select the parent node, the solution is the same as the above two questions. The solution report here 5, HDU 4003 find metal mineral (recommended, good question) tree DP + selected group backpack with only one item can be selected, the state transition equation is hard to think about. The solution report is here 6. poj 2486 apple tree DP + grouping backpack, but the state transition equation should be divided into three steps, which is difficult to think about. Solution Report: Here 7, poj 3345 bribing FIPA tree DP + grouping backpack, there is no special difference compared with the previous questions, just pay attention to the input. VisibleHere8. HDU 4044 geodefense tree-like DP + grouping backpack requires that the smallest HP be obtained from each son node, and then find the largest combination of these sons. For details about the solution report, see here 9, zoj 3627 treasure hunt II tree-shaped DP + grouping backpack, the water question of the Zhejiang University and the month competition, a very common tree-shaped backpack.
10. 4276 the Ghost Blows light can be written in two ways: one is to compress a tree into a chain and then DP on the chain, and the other is similar to the apple tree, for details, see here 3. Delete vertex or delete edge class tree DP.
1. HDU 3586 information disturbing dual upper power limit, which is then updated from the leaf node, and the edge weight is compared with the limit before status transfer. Solution Report: here 2, poj 3107 Godfather Delete points, minimize the maximum number of nodes in the remaining branches, perform a deep search to record the distance to the leaf node, and then perform enumeration to obtain the maximum value, then update the answer. Solution Report: here 3. poj 2378 tree cutting Delete points, so that the number of branches with the maximum number of nodes in the remaining branches is equal to half of the total number. There are several solutions, which are similar to the previous question. The solution report here 4 and poj 1655 balancing act Delete points minimize the maximum number of nodes in the remaining branches, perform a deep search to record the distance to the leaf node, and then perform enumeration to obtain the maximum value, then update the answer. In the solution report here 5 and poj 3140 contestants division, find the minimum difference between the two branches after deleting an edge, which is also a two-step in-depth search. Solution report here
This article will be updated constantly. I will organize this topic every time I encounter a tree-like DP in the future. I hope you will stay focused on this topic. This article is original in zeroclock, but it can be reproduced, because we are brothers.