This article is about the solution to a recent problem from the CCF NOI 2018 Winter Camp, the penultimate stage before the Chinese national team is selected. I believe that the solution contains a few techniques that are rarely known.
Short description of the problem:
You are given three trees T 1, T 2, T 3 with the same number of nodes. Let d 1(x, y), d 2(x, y), d 3(x, y) denote the distance from x to y in each tree. Find a pair of nodes (x, y) such that d 1(x, y) + d 2(x, y) + d 3(x, y) is maximized. Output that value.
Let's start by solving the problem for two trees only. Let f 1(v) denote the distance from node 1 to node v in the first tree. We want to maximize . For each node v in the second tree, let's add a node v' and an edge (v, v') to the second tree, with weight f 1(v). If we fix , we have reduced our problem to this: "Given two sets of nodes A, B, merge them and calculate the maximum value of , where d'2(a, b) denotes the distance between a and b in the modified second tree." It can be proven through a reductio ad absurdum argument, that for each one of these sets, we only need to store a single pair of nodes (a, b) such that d'2(a, b) is maximized. This means that merging is done in this way: Let (a 1, b 1) and (a 2, b 2) be the aforementioned pairs. The value of the new pair will be (a 1, a 2), (a 1, b 1), (a 1, b 2), (a 2, b 1), (a 2, b 2), or (b 1, b 2), depending on which pair maximizes d'2(a, b). This gives us an algorithm.
Now, let's solve another version of the problem: We have three trees, but the second and third trees are path-shaped. Again, let's fix . We have to maximize f 1(x) + f 1(y) + |f 2(x) - f 2(y)| + |f 3(x) - f 3(y)|. For each subtree of the first tree, we will maintain four values:
Since , the best answer will be either equal to g 1, 1(x) + g 2, 2(y) or g 1, 2(x) + g 2, 1(y) for some pair of nodes (x, y). This gives us a simple O(n) solution if we keep the prefix and suffix maxima of g for the children of every node.
Unfortunately, we still have one more subtask to solve before we reach the full solution. In this subtask, we are given two trees and a path. Let's start by introducing the auxiliary tree: Let A be a subset of nodes of a tree T. Then, the auxiliary tree contains the nodes , and every node is connected to its closest ancestor in the original tree with an edge of weight . The auxiliary tree therefore has O(|A|) nodes. Now, we can do D&C on the path: Let's fix its midpoint m. We have to maximize d 1(x, y) + d 2(x, y) + d 3(x, m) + d 3(m, y), where x ≤ m and y ≥ m. If we add an extra vertex v' to T 2 where the weight of the edge (v, v') is equal to d 3(v, m) and create the auxiliary trees that only contain the vertices in the range we're currently processing, we can use the solution to the "two trees" subtask to solve this one too. This gives us an solution, with an albeit difficult implementation.
And finally, we can move on to the complete solution. But first, we have to introduce another technique, called "centroid decomposition on edges". Here is a simple pseudocode description of this technique:
Obviously, this will work in O(n 2) in some cases, e.g. a star graph. However, if we have a binary tree, this code works in O(nlogn). We can make an arbitrary tree binary while preserving distances between nodes using a method similar to the left-child right-sibling conversion. If we have a node with k > 2 children, we are going to attach a single child node to it. This node's left child will be the former first child of the node, and it will be connected to it with a weight of identical edge. Its right child will be its "sibling" node, whose left child will be the former second child of the node, and so on.
Our full solution is simply a combination of these two techniques. Make the first tree binary, and do "centroid decomposition on edges" on it. Similarly to the previous subtask, add a dummy node x', such that d'2(x, x') = h(x), where h(x) is the distance from node x to the active edge in the first tree. Then, solve the "two trees" subtask. This gives us an solution.
If you find any mistakes in this solution, comments are welcomed.