Coin type identification
Time limit2sMemory limit512 MB
Determine each coin fixed type from the pairwise weighing results, printing ? when it is not unique.
- Level
Medium7 of 10
- Topics
- Union-find, Topological sort, Dynamic programming, Graph
- Solved
- No attempts yet
Problem
Mirko visited a distant country where nobody uses banknotes, only coins. The country has types of coins in circulation, named K1, K2, K3, ..., KN. All coins have the same size and shape, but their weights differ. K1 is the lightest type, K2 is the second lightest, and so on up to KN, the heaviest type.
Mirko has coins in his pocket and does not know which type each one is. The only tool he has for finding out is a simple balance scale.
Mirko first labelled his unknown coins with the numbers through , and then performed weighings. In one weighing he puts one coin on one side of the scale and another coin on the other side. He then sees whether the two coins weigh the same, and if they do not, which one is heavier.
Write a program that uses the weighing results to determine the type of every coin whose type is fixed by those results.
Input
The first line contains the integers , and : the number of coin types in the country, the number of coins in Mirko's pocket, and the number of weighings.
Each of the next lines holds the result of one weighing in the form ACB, where and are different positive integers not greater than , and is the character = (equal) or < (lighter).
There is no space between the numbers and the character . One weighing result says that Mirko's coin numbered weighs the same as the coin numbered , or is lighter than it.
The weighing results are never contradictory.
Output
Print lines. Line must contain the type of the coin numbered , written as KX, where is an integer between and .
If the type of the coin numbered cannot be determined uniquely, print the character ? on line .
Constraints
In all subtasks, , and .