Editorial for CEOI '17 P6 - Chase
Submitting an official solution before solving the problem yourself is a bannable offence.
What is the maximal difference in the number of pigeons we can make by dropping some breadcrumbs while running through the park? It is obvious from the description that the park represents a tree where each statue is a node and each path is an edge.
Exhaustive search. The tree has possible paths in it, each determined by its beginning and end. On a path, we have possible ways to drop the crumbs. The exhaustive search can simulate all the possible strategies in time complexity.
Greedy approach. To approach the problem better, we must first make some observations. Let us say Jerry is currently stationed in node and arrived here from node . Jerry will visit node in the next step. Let us denote number of pigeons in all the neighbours of the node . The gain from dropping a breadcrumb is regardless of where the other breadcrumbs were dropped.
If a node is neighbour to the and , the observation is quite straightforward. If we drop the breadcrumb at node , pigeons will be added to the path Tom traverses, otherwise not. Pigeons from node do not affect Jerry's path, nor do they affect Tom's path. Jerry already met them once (and won't meet them again, even if they moved to node ) and Tom will only meet them once regardless of their position. Pigeons from node move to node , which means they won't count toward a total number of pigeons Jerry met. They will count towards the total number of pigeons Tom met. The values that counted toward gain have not been changed from the start. Jerry has not yet reached these nodes or any of their neighbours. Hence the gain of dropping a breadcrumb at a node only depends on the last visited node.
If the starting node of a walk is known in advance, all the gains can be calculated in advance. Now all we have to do is determine an ending point in such a way that the sum of the top gains will be maximal. This can be done with a depth first search by storing all the gains on the current path in a priority queue and selecting the top . This approach takes time if we know the starting node or if we have to try every possible initial node.
Dynamic programming. Let us root the tree in node . Let us denote maximal difference we can make by starting a path in node and continuing in one of its subtrees, dropping at most breadcrumbs in the subtree. Let us denote maximal difference we can make by starting a path in a subtree of a node and ending it in , dropping at most breadcrumbs in subtree or at node . Values of these two can easily be computed from values of and of children. denotes the children of node .
The best path running through node and its subtrees makes the difference . Note that the path must not begin and end in the same subtree. A way to achieve this is to temporarily store the best two values for each possible and in which subtree that path started/ended. The best solution which does not use the same subtree twice can be easily calculated from these. This approach takes time.
Comments