Statement

You are given a rooted tree with 𝑛8000 nodes and height 𝑚800. Assign integer 𝑤𝑖 to every node such that the value of the tree is maximized, where value is defined as

𝑢mex𝑣𝑆𝑢 𝑤𝑣

and 𝑆𝑢 denotes the nodes in the subtree of 𝑢. (https://qoj.ac/contest/2669/problem/15459)

Solution

𝒪(𝑛3)

The simplest DP state is to let dp[u][m][f] denote the maximum value of subtree 𝑢 given that the MEX of this subtree is 𝑚 and there are 𝑓 “free” values that don’t contribute to any mex.

1(w=3)2(w=0)3(w=1)4(w=2)

For instance, consider the above tree. The (𝑚,𝑓) states of subtrees 2, 3, and 4 are (1,0), (0,1), and (0,1) respectively. When we combine these three states along with the (0,1) state of our root, the new resultant state is (max(1,0,0,0),0+1+1+1)=(1,3). Then, we can use the 3 free values 𝑤1,𝑤3,𝑤4 to increase the mex of our subtree, leading to a final state of (4,0).

Therefore, the most naive method of transitioning is essentially a convolution where (𝑚1,𝑓1)+(𝑚2,𝑓2)=(max(𝑚1,𝑚2),𝑓1+𝑓2). Then, we can use some of our free values to increase our mex: that is, (𝑚,𝑓) transitions to (𝑚+𝑘,𝑓𝑘).

Naively implementing this is very slow, but we can actually speed this DP up to 𝑂(𝑛3) by simply transitioning (𝑚1,𝑓1)+(𝑚2,𝑓2) to both (𝑚1,𝑓1+𝑓2) and (𝑚2,𝑓1+𝑓2). This corresponds to fixing one subtree to provide the maximum MEX and ignoring the MEX of the rest, which allows for our transitions to be optimized via pre-computation. For more details, see my implementation here.

𝒪(𝑛𝑚2)

It seems like the most difficult decision to make in our DP is picking which subtree to provide the maximum MEX. Let’s consider fixing our choices for every node first, then optimizing based on that. For each node, we will draw a red edge to the child subtree with the maximum MEX.

bg-white

As described in our 𝒪(𝑛3) DP, if 𝑣 is the red child of 𝑢, mex𝑢=mex𝑣+|𝐹𝑢|, where 𝐹𝑢 is the set of nodes consumed by 𝑢 to increase its MEX. For instance, in the first tree shown, 𝐹1={1,3,4}, 𝐹2={2}, and 𝐹3=𝐹4={}. If 𝑣 is in 𝐹𝑢, we will refer to node 𝑣 as a stepping stone for node 𝑢.

Since we’ve fixed the red edges, our job is now to decide the 𝐹 sets. To do this, we analyze the contribution of each individual node. Consider node 16. It can used as a stepping stone for any of its ancestors (including itself), so which one should we pick?

Note that if we choose to use node 16 as a stepping stone for node 10, it will not only increment mex10, but also mex9, mex8, and mex7, since they connect to node 10 via red edges. Therefore, if 16 is placed in 𝐹10, it will have a contribution of +4. In comparison, placing it in 𝐹3 only contributes +3, and placing it in 𝐹11 or 𝐹16 only contributes +1.

We can thus define a new DP state: dp[u][mx][len] denotes the maximum possible value of subtree 𝑢 given that node 𝑢 can contribute a maximum of +mx and is at the end of a red chain with len nodes. For instance, node 8 in the above diagram would be in the state dp[8][3][2]. The 3 represents that node 8 can have contribution +3 by becoming a stepping stone for node 3, and the 2 represents the current length of node 8‘s chain (consisting of nodes 7 and 8). Transitions are left as an exercise to the reader.

𝒪(𝑛𝑚)

A good thing about reformulating our DP in this way is that it admits an important greedy observation. Let 𝑢 be a blue child (e.g. node 7 or 15 in above diagram), and mx be its maximum possible contribution (same as our DP above). Then, one of the following two conditions must be true:

  1. All red chains in the subtree of 𝑢 have no more than mx nodes.
  2. The red chain rooted at 𝑢 has more than mx nodes.

To prove this, consider the case where the red chain rooted at 𝑢 has fewer than mx nodes, but one of its children starts a chain with more than mx nodes. Then, it’s never less optimal to simply connect this child to node 𝑢 instead.

TO BE CONTINUED

My submission: https://qoj.ac/submission/1881469