The Worst Reporter
InterviewTime limit1sMemory limit128 MB
Given some match results of a round-robin where higher-ranked teams always win, find the lexicographically smallest ranking and say whether it is unique.
- Level
Medium6 of 10
- Topics
- Graph, Topological sort, Greedy, Sorting
- Solved
- No attempts yet
Problem
You are a reporter at a newspaper, covering sports.
Up until yesterday, a round-robin league of soccer teams (every team plays every other team exactly once) took place. Based on the results and the rules, the organizing committee assigned each team a distinct rank from to . You were told the outcomes of some of the matches, together with the following facts.
- Fact 1: No match ended in a draw.
- Fact 2: Every team received a distinct rank.
- Fact 3: For every , with , in the match between the team ranked and the team ranked , the team ranked always won. (That is, a higher-ranked team always beats a lower-ranked team.)
Using the known match outcomes and Facts 1-3, reconstruct one ranking table consistent with the given information, and also determine whether any ranking table other than the one you output is also consistent with the information.
A ranking table is the list of teams from rank to rank .
Because several consistent ranking tables may exist, output the lexicographically smallest one. (Compare the sequences of team numbers listed from rank to rank and choose the sequence that is smallest in dictionary order.)
Input
The first line contains the number of soccer teams . Each team is numbered from to .
The second line contains the number of known match outcomes .
Each of the next lines contains two integers and separated by a space, meaning that team beat team .
The values satisfy and .
Output
The output consists of lines.
On lines through , output a ranking table consistent with the information. On line (), output the number of the team ranked . If several ranking tables are consistent, output the lexicographically smallest one.
On line , output an integer indicating whether a ranking table other than the one you output is also consistent with the information. Output if none exists (the ranking table is unique), or if one exists.