Broken Gearbox
Time limit2sMemory limit512 MB
Assign each of n gear radii to a vertex of a connected graph so that every edge's distance equals the sum of its assigned radii, returning the lexicographically smallest assignment or impossible.
Problem
The Mechanical Turk was an 18th-century fake robot that created the illusion of artificial intelligence by playing chess. It inspired us to build our own fake robot, one that creates the illusion of real intelligence by solving programming contest problems.
To make the machine look more convincing, we mounted gears on axles inside an uncovered panel. The gears are purely decorative, so we placed them to form an impressive meshing pattern without any regard for gear ratios or turning direction. It is entirely possible that none of the gears can actually turn.
It is guaranteed that every axle was connected to every other axle by meshing, either directly or indirectly. Two axles and have directly meshing gears when their distance equals the sum of the radii of their gears, that is, .
Sadly, the gears fell off the machine. We believe we collected all of them, but we no longer know which gear belongs on which axle. Find a way to put the gears back on the axles so that they mesh the way they did originally.
Input
The input consists of:
- One line with an integer (), the number of gears and axles.
- One line with integers (), the radius of each gear.
- One line with an integer (), the number of pairs of axles to mesh.
- lines, each with three integers , , (, ): the indices of two axles that were directly meshed, and the distance between them.
Output
If the machine can be fixed with the given gears, output integers on one line, separated by spaces, where is the 1-based index of the gear to put on the -th axle. The sequence must be a permutation of to . If several valid placements exist, output the lexicographically smallest sequence : at the first position where two sequences differ, the one with the smaller gear index comes first. Otherwise, output impossible.