Editorial for CEOI '22 P3 - Prize
Submitting an official solution before solving the problem yourself is a bannable offence.
Prepared by: Ivan Paljak and Josip Klepec
Setup
Summarization of the task is somehow choosing the node labels and then queries such that they uniquely determine a tree.
(DMOJ editor's note: Go here to read Algorithm 1)
The subset of node labels forms a connected subgraph in tree . In tree it is just some arbitrary subset of nodes.
(DMOJ editor's note: Go here to read Algorithm 2)
Each query may also give some lowest common ancestor which is not in .
We define and , a subset of nodes in respective trees, as the union of and lowest common ancestors that appear in queries.
Lemma 0.1. can be formed into a tree structure. Furthermore, forms a connected subgraph in tree but it isn't relevant for the rest of the algorithm.
We will describe the algorithm for making such small tree. It can be applied to both and .
(DMOJ editor's note: Go here to read Algorithm 3)
Now for these trees, we want to reconstruct the weights of edges. For each tree, we have paths we know the length of. Some may be redundant.
Lemma 0.2. Those paths are such that they uniquely determine the tree.
The proof is not too hard so we won't go into too much detail. Firstly, imagine we construct a graph as described in section for Full Points (see section couple lines below).
Now, all we need to prove is that that graph is connected. There is no need to prove that the given set of queries doesn't give a contradiction because we can rely on the authentication of the interactor.
We can prove the connectivity by analyzing how we constructed the queries. The connectivity becomes very obvious very quickly after we realize that we first sorted the node labels and then asked of adjacent ones. Every pair of adjacent node labels being connected means the whole set is.
Subtask 3
Even though it is not necessary for full points we can use Gaussian elimination to solve this subtask. Every query forms some equation (with variables being edge weights in respective trees). Now we get two linear systems, one for each tree and we just solve each independently.
https://cp-algorithms.com/linear_algebra/linear-system-gauss.html
Subtask 4
It is not hard to modify the algorithm for constructing the small tree to reconstruct the weights as well if our queries are also sorted by the preorder. But it only holds for the second tree. To achieve this for both trees we need to find a set of node labels such that they can be ordered in a way that they form a monotonic sequence with respect to the preorder in the first tree, but also in the second tree.
One could always choose such a subset because holds.
If we order node labels with respect to preorder in tree and look at the sequence of their preordering positions in the tree . We can find a monotonic subsequence of length .
Theorem 0.3 (Erdos-Szekeres). For given positive natural numbers , any sequence of distinct real numbers whose length is at least contains a monotonically increasing subsequence of length or a monotonically decreasing subsequence of length (or both).
Full Points
Method 1
For a rooted tree we denote as the length of the path that starts at the root of the tree and ends at .
Then our queries give equations of the form:
Now, we construct a weighted directed graph by adding an edge from to with weight and a reverse edge with negative weight .
Now to find , we just find the length of the path from the root to in our new graph using dfs. Such a path is guaranteed to exist because of the structure of queries.
When we reconstructed for every it is easy to output the answers.
This gives us an algorithm with complexity .
Method 2
Alternatively, one could solve the system of equations by using Gaussian elimination faster by observing that each equation forms a path in a tree. Furthermore, a path goes from some node to the root of the tree (meaning lca of endpoints of the path is always on them).
(DMOJ editor's note: Go here to read Algorithm 4)
At first, this gives us complexity , but it can be sped up to by keeping the set of equations that begin at every node and merging those sets when subtraction equations. If we merge the two sets such that we move everything from smaller set to larger we get the desired complexity. It is convenient to keep equations in stl Set structure to get the minimum path.
Comments