Under construction

Still being built. Things may change

Book Reviewers

Interview

Time limit6sMemory limit1024 MB

Summary
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 NN books. Each review ends with the book being given a rating on a scale from 11 to MM. 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 11 to NN and wants to determine their ratings a1,a2,…,aNa_1, a_2, \dots, a_N. To do this, the reviewer has made RR comparisons that describe the relation between aia_i and aja_j for some pairs of books i,ji, j.

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 NN (1≤N≤1000001 \leq N \leq 100000), MM (1≤M≤1000001 \leq M \leq 100000), and RR (1≤R≤5000001 \leq R \leq 500000), which are the number of books, the highest possible rating, and the number of comparisons.

Then follow RR lines, each describing a relation that must hold. Each line has the format <i> <relation> <j>, meaning that aia_i <relation> aja_j must hold. ii and jj are integers between 11 and NN, with i≠ji \neq j. The relation is one of the strings <, =, or ≤. No pair of books is compared more than once.

Output

Print a list of integers a1,a2,…,aNa_1, a_2, \ldots, a_N such that every relation holds and every number lies in the range [1,M][1, M]. If there are several solutions, print any of them. If it is impossible, print −1-1.

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 [1,3][1, 3] and that they satisfy the four relations a1<a2a_1 < a_2, a2<a4a_2 < a_4, a3<a2a_3 < a_2, and a2<a5a_2 < a_5.

Examples4

  1. Example 1

    Input
    5 3 4
    1 < 2
    2 < 4
    3 < 2
    2 < 5
    
    Expected output
    1 2 1 3 3
    
  2. Example 2

    Input
    3 10 3
    1 < 2
    2 < 3
    3 < 1
    
    Expected output
    -1
    
  3. Example 3

    Input
    6 4 6
    2 < 1
    3 = 1
    6 = 3
    6 < 5
    5 < 4
    1 < 4
    
    Expected output
    2 1 2 4 3 2
    
  4. Example 4

    Input
    7 3 8
    1 <= 2
    5 = 7
    2 <= 7
    6 < 1
    5 <= 1
    2 < 3
    6 <= 4
    4 = 3
    
    Expected output
    2 2 3 3 2 1 2