Good question: tree dp+change root-A-wood forest

Portal: https://ac.nowcoder.com/acm/contest/6874/A

This question is probably the first dp question I did. It's actually quite easy

The main idea of ​​the topic: For each point, find the minimum cost of traversing the complete graph from this point.

Ideas:

The first point: If we ask to return to the starting point, then the cost is [Bian Quan Cui sum* 2]. Not asking to return to the starting point is equivalent to choosing a path. The weights on this path are only added once. The other sides are still added twice.

Then we must choose the farthest point from this point as the final path. Then the problem is transformed into the problem HDU2196. Find the distance of the furthest point from each point on the tree.

My thoughts:

1. First pass dfs to find the answer in each point subtree.

2. In the second pass of dfs, pass a value from the parent node. Then save (with each child as the farthest distance of the subtree + w[i], child number) in the form of a two-tuple. Sort in descending order. Then when transferring to the child, greedy first chooses the value of first as large as possible and second != v(i) to the child. 

Guess you like

Origin blog.csdn.net/qq_35577488/article/details/108575694