Book Reviewers
InterviewTime limit6sMemory limit1024 MB
Assign ratings from 1 to M to N books so that every <, =, or ≤ comparison holds, or report -1 if no assignment exists.
- Level
Medium6 of 10
- Topics
- Topological sort, Union-find, Dynamic programming
- Solved
- No attempts yet
Problem
A book reviewer has to review books. Each review ends with the book being given a rating on a scale from to . Choosing an absolute rating for each book directly is hard, so the reviewer finds it much easier to compare two books at a time and state which one is better.
The reviewer has numbered the books with integers from to and wants to determine their ratings . To do this, the reviewer has made comparisons that describe the relation between and for some pairs of books .
The reviewer is satisfied with any rating assignment as long as all requirements from the comparisons are met. Help the reviewer find such an assignment.
Input
The first line contains three integers (), (), and (), which are the number of books, the highest possible rating, and the number of comparisons.
Then follow lines, each describing a relation that must hold. Each line has the format <i> <relation> <j>, meaning that <relation> must hold. and are integers between and , with . The relation is one of the strings <, =, or ≤. No pair of books is compared more than once.
Output
Print a list of integers such that every relation holds and every number lies in the range . If there are several solutions, print any of them. If it is impossible, print .
Hint
In the first sample input, 1 2 1 3 3 is a valid solution. This can be verified by checking that all numbers lie in and that they satisfy the four relations , , , and .