Editorial for IOI '15 P6 - Towns
Submitting an official solution before solving the problem yourself is a bannable offence.
The graph given in this task is an edge-weighted tree , in which the degree of each internal node is at least three. Let denote the number of leaves of . The goal is to find the centers of the tree and determine if any center is also a leaf median (an internal node such that after removing the node, every component contains at most leaves. In this task, the tree is not given, and the contestants must solve the task by using a limited number of queries for distances between leaves. An algorithm using queries is sketched as follows. The details are in the next sections.
- First, find the centers by at most queries; and then
- for each center (at most two centers), determine if it is also a leaf median by using no more than queries.
Radius and centers
The diameter of a tree can be found with queries as follows.
- Farthest to Farthest: Pick an arbitrary vertex and find a vertex farthest to . Find a vertex farthest to . It can be shown that is a diameter of the tree.
- Any center must be one the -path.
- Then the vertices on the -path with its distance to closest to are centers.
Determining if a center is also a median
Let be the two leaves in the above process and be a center on the -path with . We need to determine if each component of has at most leaves. Let be the set of all leaves.
First we compute the multiset . Each of the different values in identifies a unique internal vertex on the -path. Let be the internal vertex on the -path with , where is the median of the multiset . If we root at the -path, the leaf median must be in the subtree rooted at . Therefore if (i.e., ), then is not a median. Note that there are two medians in if is even. Otherwise it remains to solve the "giant component" problem: checking if there is a component in with more than leaves.
Let which is the set of the leaves branching from -path at . For , we have that are in the same component of iff . Then, we can solve the giant component problem by algorithms for the following problem: There are color balls and we want to determine if there are more than balls of the same color. The cost is counted by the number of performed queries which returns Equal/NotEqual according to if and are of the same color.
It was shown that queries are necessary and sufficient in Fischer and Salzberg (1982), Solution to problem 81–5, J. Algorithms, pp. 376–379. The following is another similar approach. Initially each element is itself a set. At each iteration, we arbitrarily pair up the survival sets and compare their representatives. If they are equal, then join the two sets into their union. Otherwise, mark both dead. In the case that the number of alive sets is odd, mark anyone dead. Repeating this process, eventually either there is exactly one survival set or all sets are dead. It can be shown that if there is a majority originally, then it must be the survival one. So the remaining work is to compare the survival representative with the representatives of all DEAD sets.
The number of comparisons: Let denote the number of alive sets in the iteration (). The number of comparisons to obtain the only survival set is . In the second stage, the number of comparisons is the number of dead sets. Let denote the number of sets die at iteration . Then, we have . Similarly . Therefore, the total number of dead sets is . In summary, the number of comparisons is .
Comments