Prepare lawyers
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Zenyk lives in a country with a very interesting history. This country has existed for \(h\) years. In the first year of its existence, only one city was built — the capital. Each subsequent year, for each city built in the previous year, two additional cities were built, which were connected to it by bidirectional roads. Thus, after \(h\) years, the country had exactly \(2^h-1\) cities and \(2^h-2\) roads.
Recently, some imp "to improve the country’s infrastructure" built exactly three additional roads between certain pairs of cities. By doing this, in Zenyk’s opinion, all the beauty of the country was lost, and he decided to file a lawsuit against this person.
Zenyk has a list of all the roads in the country, but he does not know when each road was added, nor how the cities are numbered. In order to prepare the lawsuit, he needs to know which three of these roads could have been added last. Help him find them.
Input
The first line contains a single integer \(h\) — the number of years Zenyk’s homeland has existed.
The next \(2^h+1\) lines contain pairs of integers, one pair per line. Each of these pairs denotes the numbers of the cities connected by the corresponding road.
Cities are numbered with integers from 1 to \(2^h-1\) inclusive.
Roads are numbered with integers from 1 to \(2^h+1\) inclusive, in the order of the input data.
Output
In a single line, output three distinct integers separated by a space — the numbers of the roads that could have been added last.
If there are multiple possible answers, first minimize the first number, then minimize the second, and then — the third.
If no answer exists, i.e., the list of roads is invalid, output
-1.
Constraints
\(2 \le h \le 10\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 3 1 6 3 5 2 4 2 7 3 4 7 3 1 3 5 1 4 7 | 2 3 5 |
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Prepare lawyers - Editorial
A correct list of roads must form a full binary tree of height \(h\) - 1 plus 3 extra edges that we need to find. Since each of the three extra edges forms a cycle in the tree, only edges that are not bridges of the graph can be candidates. Let’s find all such edges. Since the total number of edges does not exceed 1025, we can find all bridges in \(O(n^2)\) (where \(n\) is the number of vertices) by simply removing the edges one by one and checking connectivity. This gives the set of candidates.
Since the diameter of a full binary tree does not exceed \(2 \cdot h\), adding one edge to the tree
creates a cycle of length at most \(2 \cdot
h\). So when three edges are added, the total number of edges in
cycles does not exceed \(6 \cdot h\).
Under the constraints, \(6 \cdot h \le
60\). So if the set of candidates has more than 60 edges, the set
of roads is certainly incorrect and the answer is -1.
Otherwise we go over all triples of different candidate edges in
lexicographic order and check whether removing them from the graph
leaves a full binary tree.
To check whether a graph is a full binary tree, we can use the following criterion. The graph must have exactly one vertex of degree 2, the root, and exactly \(2^{h - 1}\) vertices of degree 1, the leaves. All other, intermediate, vertices must have degree 3. It remains to check that the graph is connected and that the height of the tree is \(h - 1\). For this we can run a depth-first search from the root, keeping the maximum depth among all visited vertices and their number.
This gives an algorithm that works in \(O(n^2 + n \cdot h^3)\).
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|