#C12256. Compute Player Ranking

    ID: 41663 Type: Default 1000ms 256MiB

Compute Player Ranking

Compute Player Ranking

You are given the results of several head-to-head matches between players.

Each match result contains two names: the winner and the loser. The objective is to compute the overall ranking of the players using the following criteria:

  • Players with more wins are ranked higher (in descending order of wins).
  • If two players have the same number of wins, the player with fewer losses is ranked higher (in ascending order of losses).
  • If both wins and losses are equal, players are ordered in lexicographical order of their names.

Formally, if a player i has wi wins and li losses, then the ranking is determined by sorting the players according to the tuple $$(-w_i, l_i, \text{name})$$.

Input is taken from standard input (stdin) and output is printed to standard output (stdout).

inputFormat

The input begins with an integer n denoting the number of matches. Each of the following n lines contains two space-separated strings representing the winner and the loser of a match.

For example:

6
Alice Bob
Bob Charlie
Charlie Alice
Alice Charlie
Bob Alice
Charlie Bob

outputFormat

Output a single line containing the players' names in their ranking order separated by a single space.

For example, for the input above, the output is:

Alice Bob Charlie
## sample
6
Alice Bob
Bob Charlie
Charlie Alice
Alice Charlie
Bob Alice
Charlie Bob
Alice Bob Charlie