Triball Ranking
InterviewTime limit2sMemory limit512 MB
Order all k players to satisfy every match result and return the lexicographically smallest lineup, or 0 when no lineup fits.
- Level
Medium4 of 10
- Topics
- Topological sort, Graph, Heap
- Solved
- No attempts yet
Problem
Triball is a sport proposed for the Youth Olympic Games in Singapore. It is played by players numbered through . Every match puts three distinct players on the court, and the result names either the one player who won or the one player who lost.
Say match is played by , , . A result written as means won, so is better than and better than . A result written as means lost, so is better than and is better than .
You are given the results of matches. Line up all players so that a better player always comes before a worse one. When more than one lineup satisfies every result, answer with the lexicographically smallest one, comparing lineups as sequences of player numbers. When no lineup satisfies every result, print .
Input
The first line contains the number of players and the number of matches , separated by a space. (, )
Each of the next lines holds one match result. A line is written without spaces as p1>p2,p3 or as p2,p3>p1, and the three players in a match are distinct.
Output
If a lineup satisfying every result exists, print the player numbers of the lexicographically smallest such lineup on one line, separated by single spaces. Otherwise print .