Basics

First, consider how we might solve the problem if 𝑋=𝑌. Actually, if we let 𝑑(𝑢,𝑣) denote the distance between nodes 𝑢 and 𝑣, it turns out the following algorithm works:

In order of increasing 𝑑(𝑢,𝑋) and while we have not exceeded the 𝐾 threshold, set the closing time of node 𝑢 to be 𝑑(𝑢,𝑋).

Proof. We can use a bounding argument.

  1. If we want 𝑘 nodes to be reachable from 𝑋, then the closing times of each of these nodes 𝑢𝑖 must be at least 𝑑(𝑢𝑖,𝑋); therefore, the answer is lower-bounded by the sum of the 𝑘 smallest 𝑑(𝑢,𝑋).
  2. At the same time, our construction guarantees that all 𝑘 nodes can be visited. Roughly, this is because if we select all nodes with 𝑑(𝑢,𝑋)<𝐷 for some threshold 𝐷, there necessarily exists a path using only these nodes to any node with 𝑑(𝑢,𝑋)=𝐷 (by definition of distance).

(note that this approach for 𝑋=𝑌 should apply to general graphs as well, not just trees)

An illustration of this strategy. The red numbers represent 𝑑(𝑢,𝑋) for each node, and the shaded nodes are the ones for which we set the closing times to 𝑑(𝑢,𝑋). All other closing times are 0.

Subtask 1

Now, let’s consider the first subtask in which the distance between 𝑋 and 𝑌 is greater than 2𝐾. This means that no node can be reachable from both 𝑋 and 𝑌, which means we can essentially reuse our solution for 𝑋=𝑌! In particular:

  • Let 𝑐(𝑢)=min(𝑑(𝑢,𝑋),𝑑(𝑢,𝑌)).
  • Like before, sort by 𝑐(𝑢) and take until we can’t.

The proof of correctness is essentially the same as in the 𝑋=𝑌 case.

Again, red numbers represent cost and shaded nodes are the reachable ones. In this scenario, the best answer is therefore 5.

Line

Now, consider the case in which the tree is a single line, and 𝑋 and 𝑌 are at its ends (but the distance between them can now be less than 2𝐾). Now, a node 𝑢 could be reachable from both 𝑋 and 𝑌 as long as its closing time is at least 𝐶(𝑢)=max(𝑑(𝑢,𝑋),𝑑(𝑢,𝑌)). Thus, we now have to decide for each node whether its reachable zero times, once, or twice (corresponding to costs 0, 𝑐(𝑢), and 𝐶(𝑢) respectively).

A possible solution. The square means we want this node to be reachable from both 𝑋 and 𝑌, and the blue costs represent 𝐶(𝑢). Therefore, the total cost of this configuration is 0+2+6+2+0=10𝐾. The total convenience is 1 + 1 + 2 + 1 + 1 = 6.

First, note that in order for any node to be a square, all nodes on this line must at least be shaded circles first (some of which we can then “upgrade” to squares). Moreover, the nodes upgraded to squares must start from the midpoints and propagate outward. For instance, in the example above, it’d be invalid to have only the second node from the left as a square, and the rest as circles.

The midpoints are circled in green (lengths are the same as in the above image). Note that if the second-from-left node is a square, the third-from-left node must also be a square. Therefore, the configuration shown here is invalid.

However, just like before, it turns out that any optimal configuration is actually valid!

Proof. Consider the cost of upgrading each node, 𝐶(𝑢)𝑐(𝑢). Plotting, it should look something like this:

The cost will decrease until the first midpoint, then increase after the second midpoint (costs are shown above the graph in black, and the order of upgrades is indicated by the green numbers). Therefore, upgrades always radiate outward from the center, which corresponds to a valid solution.

Thus, our final algorithm is as follows:

  1. First, upgrade nodes to shaded circles in order of increasing 𝑐(𝑢) while we still can.
  2. If any nodes remain unshaded, terminate.
  3. Otherwise, upgrade circles to squares in order of increasing 𝐶(𝑢)𝑐(𝑢).

The final answer is (# of shaded circles)+2(# of squares).

Tree

To generalize our line solution, we claim the following solution works for any tree:

  1. First, find the answer under the assumption that no node is reachable twice (this is the Subtask 1 solution).
  2. Otherwise, assume some node is reachable twice: this means that all nodes on the path from 𝑋 to 𝑌 must be upgraded at least once.
  3. From here, make any additional upgrades: we claim that any choice of upgrades that minimizes total cost necessarily corresponds to a valid solution.

If the claim in point 3 is true (which we will prove momentarily), the problem reduces to the following:

You are presented with 𝑛 boxes. From box 𝑖 you can take zero items for cost 0, one item for cost 𝑐𝑖, and two items for cost 𝐶𝑖. it is guaranteed 𝑐𝑖𝐶𝑖. Find the maximum number of items that can be taken given that the total cost must not exceeded 𝐾.

To solve this, we can split boxes into two types:

  1. 𝑐𝑖𝐶𝑖𝑐𝑖. We can split this box into two single-item boxes with cost 𝑐𝑖 and 𝐶𝑖𝑐𝑖 respectively. Note that we should never take the second box without taking the first box, so any valid solution with this reduction corresponds to a valid solution for the original problem as well.

  2. 2𝑐𝑖>𝐶𝑖. Note that if we take two half-boxes of this type, it’s always better to instead take the entire box with smaller 𝐶𝑖. Two half-boxes. A lower-cost solution can be achieved by making the swap indicated by the blue arrow.

    Therefore, we will take at most one half-box of this type, and the remaining full boxes should then be taken in order of increasing 𝐶𝑖. So, sorting these boxes by 𝐶𝑖, we will always take a prefix with the exception of at most one element (which should be the element with maximum 𝐶𝑖𝑐𝑖 or some later element with minimal 𝑐𝑖).

So, we can just enumerate all prefixes of type 2 boxes, as well as whether we choose one of these boxes to be a half-box, then find the maximum number of type 1 boxes (which have been reduced to single item-boxes) we can take afterward. This can all be done in 𝒪(𝑛log𝑛).


Proof of claim. We wish to show that the optimal solution found by the above algorithm (which relaxes the original problem constraints) actually always corresponds to a valid solution under the original constraints as well.

First, let 𝑆 be the set of nodes upgraded any number of times in an optimal solution.

Lemma. If the solution is optimal, then 𝑆 will be connected.

Proof. Recall that all nodes on the path from 𝑋 to 𝑌 are necessarily part of 𝑆. For other nodes in 𝑆, if its parent (when the tree is rooted at 𝑋) is not in 𝑆, it cannot be worse to replace this node with its parent.

If 𝑆 is not connected, the blue move cannot increase cost.

Now, we just need to show that the selection of squares in an optimal solution leads to a valid configuration as well. This can be shown as follows:

  1. First, decide 𝑆 and upgrade all nodes in 𝑆 to circles.
  2. Then, additionally upgrade some of these nodes to squares in increasing order of 𝐶(𝑢)𝑐(𝑢).

The key thing to note here is that within each subtree hanging off the path from 𝑋 to 𝑌, the value of 𝐶(𝑢)𝑐(𝑢) is constant.

Each subtree is shown with the upgrade cost corresponding to every node in that subtree.

Therefore, just as before, our upgrades will radiate from the center subtree outward. Furthermore, because upgrade cost is constant within a subtree, we can always upgrade in a way such that shallower nodes are upgraded first. It can be shown that these conditions will always result in a valid solution.

Code

https://qoj.ac/submission/2264940