Island
InterviewTime limit1sMemory limit128 MB
Given all pairwise shortest tolls among the n seaside triangles, recover the adjacency structure and edge weights of the underlying border tree.
- Level
Hard8 of 10
- Topics
- Tree, DFS, Graph, Implementation
- Solved
- No attempts yet
Problem
An island has the shape of a convex polygon with sides. It is divided into countries, each shaped like a triangle whose three corners are vertices of the polygon; that is, the polygon is cut into triangles by non-crossing diagonals.
No country borders exactly two other countries: every country borders either one country or three countries. Consequently there are exactly countries that border a single neighbour (the seaside countries) and countries that border three neighbours (the inland countries). The seaside countries are numbered from to , and the inland countries from to .
Crossing a border costs a toll. Different borders may cost different amounts, but crossing a given border costs the same in both directions. Each toll is an integer between and .
For every pair of seaside countries and you are told the total toll paid when travelling from to along the route that crosses the fewest borders. (Because the countries form a tree of borders, this route is unique.) From this information, reconstruct every border and its toll: for each country, find its neighbours and the toll on each shared border.
Input
The first line contains one integer (), the number of seaside countries.
Each of the next lines contains non-negative integers separated by single spaces. The -th integer on the -th line, , is the total toll on the route (crossing the fewest borders) from seaside country to seaside country . The data satisfy and , and are guaranteed to be consistent with some island whose every border toll is an integer in .
Output
Print lines describing the borders.
On each of the first lines, describe one seaside country: line (for ) contains two integers, the number of the country that borders seaside country and the toll on that border.
On each of the next lines, describe one inland country: the line for inland country (for ) contains six integers, listing its three neighbours in increasing order of neighbour number, each neighbour's number followed by the toll on the shared border.
Number the inland countries deterministically so that the answer is unique. Assign the numbers by a depth-first traversal of the border tree that starts at the inland country adjacent to seaside country : give the next unused number to each inland country the moment the traversal first reaches it, and from each inland country continue into its not-yet-numbered inland neighbours in increasing order of the smallest seaside-country number reachable through that border (without passing back through the current country).
Figure
The figure below illustrates an island of this kind: the seaside countries around the coast, the inland countries in the interior, and the borders (with their tolls) between them.
