Editorial for WC '16 Contest 4 S4 - Hopps and Wilde on the Case


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
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 1^\text{st} 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 2 at the end.

This problem can be approached with dynamic programming, split into two portions. First, let DP1[i] be the minimum amount of time required for a single cop to visit all of the required nodes in node i's subtree (starting and ending at i), with the other cop never entering said subtree. For convenience, let DP1[i] = -1 if i's subtree contains no nodes with clues. We can initialize DP1[i] to 0, and for each child c of i such that DP1[c] > -1, we can add DP1[c]+1 onto DP1[i], as the cop will need to traverse the edge down from i to c and then proceed to visit all of c's subtree's required nodes. Finally, we should remember to set DP1[i] back to -1 if C_i = 0 and DP1[c] was -1 for each child c.

Next, let DP2[i][j] be the minimum amount of time which must be spent by the first cop within i's subtree (starting and ending at i), given that the second cop is also contributing by spending j minutes within i's subtree (also starting and ending at i), and such that all required nodes in node i'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 \max(j, DP2[1][j]) across all possible values of j between 0 and N.

The trickiest part of the algorithm is actually computing the values of DP2[i][0 \dots N]. We can start by initializing DP2[i][0 \dots N] = 0, and then consider each of i's children c in turn such that DP1[c] > -1. We'll want to compute a temporary updated copy of the DP array DP2'[i][0 \dots N] (whose values should first be initialized to infinity), and then copy DP2'[i][0 \dots N] back into DP2[i][0 \dots N]. Now, there are 3 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 DP2'[i][j] = \min(DP2'[i][j], DP2[i][j]+DP1[c]+1), for all \mathcal O(N) values of j. The second option indicates DP2'[i][j+DP1[c]+1] = \min(DP2'[i][j+DP1[c]+1], DP2[i][j]). The third option indicates DP2'[i][j+k+1] = \min(DP2'[i][j+k+1], DP2[i][j]+DP2[c][k]+1) (note that, for each j, we need to also consider all \mathcal O(N) possible values of k, corresponding to how the cops may split their time in c's subtree).

The values of DP1[i] and DP2[i][0 \dots N] all depend only on the DP values of i's children, meaning that all of the DP values may be computed recursively starting from node 1. For each node i visited during the recursion, updating the value of DP1[i] based on each of i's children takes \mathcal O(1) time, and updating the values of DP2[i][0 \dots N] based on each of i's children takes \mathcal O(N^2) time. Each node is the child of at most one other node, meaning that the overall time complexity of this algorithm is \mathcal O(N^3).


Comments

There are no comments at the moment.