Editorial for WC '16 Contest 4 S4 - Hopps and Wilde on the Case
Submitting an official solution before solving the problem yourself is a bannable offence.
The network of locations and roads forms a tree, rooted at its node. Anytime Judy or Nick travel down an edge from a node to one of its children, they'll need to travel back up it later, so for convenience we can only count the downwards edge traversals and then multiply the answer by at the end.
This problem can be approached with dynamic programming, split into two portions. First, let be the minimum amount of time required for a single cop to visit all of the required nodes in node 's subtree (starting and ending at ), with the other cop never entering said subtree. For convenience, let if 's subtree contains no nodes with clues. We can initialize to , and for each child of such that , we can add onto , as the cop will need to traverse the edge down from to and then proceed to visit all of 's subtree's required nodes. Finally, we should remember to set back to if and was for each child .
Next, let be the minimum amount of time which must be spent by the first cop within 's subtree (starting and ending at ), given that the second cop is also contributing by spending minutes within 's subtree (also starting and ending at ), and such that all required nodes in node 's subtree end up getting visited by at least one of the cops. Note that the answer we're looking for is the minimum value of across all possible values of between and .
The trickiest part of the algorithm is actually computing the values of . We can start by initializing , and then consider each of 's children in turn such that . We'll want to compute a temporary updated copy of the DP array (whose values should first be initialized to infinity), and then copy back into . Now, there are types of options for this child - either the first cop should go down and handle its subtree alone, or the second cop should do so, or both cops should visit it. The first option corresponds to a recurrence of the form , for all values of . The second option indicates . The third option indicates (note that, for each , we need to also consider all possible values of , corresponding to how the cops may split their time in 's subtree).
The values of and all depend only on the DP values of 's children, meaning that all of the DP values may be computed recursively starting from node . For each node visited during the recursion, updating the value of based on each of 's children takes time, and updating the values of based on each of 's children takes time. Each node is the child of at most one other node, meaning that the overall time complexity of this algorithm is .
Comments