Editorial for CEOI '09 P2 - Harbingers


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.

A quadratic solution. The first approach is to use dynamic programming. If we consider opt_i to be the minimum time required to send a message from town i to the capital, then we have the following recursion:

\displaystyle opt_i = \min(opt_j+dist_{j \to i} V_i+S_i) \tag{1}

where j is a node on the path from town i to town 1. This relation is obtained by considering every harbinger j that may receive the message from harbinger i. By dist_{j \to i} we denote the distance between nodes j and i. This distance can be computed in constant time if we initially compute a vector D, where D_i is the distance from town i to the capital. A direct implementation of (1) takes \mathcal O(N^2) time and earns 20 points.

The special case of lines. Recurrence (1) can be rewritten as:

\displaystyle opt_i = \min(opt_j-D_jV_i+D_iV_i+S_i)

When computing the value for opt_i, D_iV_i+S_i is constant for all j, so:

\displaystyle opt_i = \min(opt_j-D_jV_i)+D_iV_i+S_i

We need to find the minimum of expressions of the form opt_j-D_jV_i. This has a useful geometric interpretation: for each node i, we consider a line in the plane given by the equation Y = opt_i-D_iX.

Finding the minimum is now equivalent to vertical ray shooting: find the lowest line in the plane intersected by the X = V_i line. To solve this query efficiently, we maintain the lower envelope of the lines. Because the road lengths are positive, we have that D_j < D_i for every i and j, where j is an ancestor of i. This means that the slopes of the lines in the envelope are descending. Thus, we can store the lines in a stack.

At each step i, we must:

  • query which line in the envelope produces the minimum for X = V_i.
  • insert Y = opt_i-D_iX in the envelope (possibly deleting others).

The first step can be solved efficiently using binary search, since the intersections of X = V_i with lines in the envelope are a unimodal function.

The second step can be solved in \mathcal O(\Delta), if \Delta lines are deleted from the envelope. We simply pop the head of the stack as long as the newly inserted line completely shadows the segment at the head of the stack. Since every line in the stack is popped from the stack at most once, the overall complexity of this step is \mathcal O(N). Thus, the entire algorithm runs in \mathcal O(N \log N) time.

The general case. To handle arbitrary trees, we implement the dynamic program by depth-first search. To maintain the lower envelope during DFS, we have to support two data structure operations:

  1. Insert a line efficiently (when moving down to a child).
  2. Delete a line and restore the envelope to the previous state (when moving up to a parent).

Point 1. cannot proceed as for line graphs, since a line can be deleted multiple times. Quadratic behavior is obtained if a node has many children and the \Delta is large between the node and each child. To insert a line in \mathcal O(\log N) time, we can once more use binary search on a unimodal function. (Solutions that do linear insertion here earn 70 points.)

For the undo operations, we maintain the lower envelope in a global array. To delete \Delta lines, we simply store the new line \Delta positions back from the top of the stack, and leave the other \Delta-1 positions intact. We also save the line that was overwritten by the newly inserted line. To restore the stack, we simply reset its top pointer, and restore the saved line to its original location in the stack. The overall complexity is \mathcal O(N \log N).

An alternative visualization of the algorithm does not use lower envelopes, but linear programming; we maintain the convex hull, and use unimodal search to find the optimum.

Though the reasoning behind the algorithm is quite complicated, the code is fairly short (~40 lines).


Comments

There are no comments at the moment.