Domino Tiles
Time limit1sMemory limit128 MB
Count pip frequencies to find the two odd endpoints of the domino chain, or report ambiguity when the chain can start anywhere.
Problem
Adrian builds a line out of domino tiles. Each tile is split into two fields, and each field shows a number of pips (dots). A tile may be rotated. Tiles are attached to either end of the line so that the two touching fields always show the same number of pips.
Adrian has tiles. Using all of them, a valid line can always be built. He wants to know the number of pips on the very first field of the finished line and the number of pips on the very last field.
Input
The first line contains a single integer (), the number of domino tiles. Each of the next lines contains two integers , (), the number of pips on the left field and on the right field of the -th tile.
Output
Print the pip counts and at the two ends of the finished line on a single line: print then when , otherwise print then (that is, print the smaller value first). If the two end values are not uniquely determined, so the result is ambiguous, print the word 'NIE' instead.