【Codeforces Round #476 (Div. 2) [Thanks, Telegram!] E】Short Code

【Link】 I am the link, click on me :)【Title】


enter question here

【answer】


First build a dictionary tree.
Obviously, some nodes will be marked.
The problem then turns into finding the minimum sum of the depths of all markers.
(Markers can be moved up, but not in the same position

Then we use the method of moving tree.
Recursively process from bottom to top.
Consider a subtree rooted at x.
If this node is marked.
Then directly add the answer to the depth of this node.

If not marked.
Then move the point with the highest depth under this subtree to this position.
Obviously, doing this greedily is optimal.

Use multiset to maintain the maximum depth below a subtree.
Then just merge the multiset with heuristic merge.
O (can pass)

【Code】

在这里输入代码

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325026930&siteId=291194637