GAME - Game schedule required

no tags 

Sheikh Abdul really loves football. So you better don't ask how much money he has spent to make famous teams join the annual tournament. Of course, having spent so much money, he would like to see certain teams play each other. He worked out a complete list of games he would like to see. Now it is your task to distribute these games into rounds according to following rules:

  • In each round, each remaining team plays at most one game
  • If there is an even number of remaining teams, every team plays exactly one game
  • If there is an odd number of remaining teams, there is exactly one team which plays no game (it advances with a wildcard to the next round)
  • The winner of each game advances to the next round, the loser is eliminated from the tournament
  • If there is only one team left, this team is declared the winner of the tournament

As can be proved by induction, in such a tournament with n teams, there are exactly n - 1 games required until a winner is determined.
Obviously, after round 1, teams may already have been eliminated which should take part in another game. To prevent this, for each game you also have to tell which team should win.

Input Specification

The input file contains several test cases.
Each test case starts with an integer n (2 ≤ n ≤ 1000), the number of teams participating in the tournament. The following n lines contain the names of the teams participating in the tournament. You can assume that each team name consists of up to 25 letters of the English alphabet ('a' to 'z' or 'A' to 'Z').
Then follow n - 1 lines, describing the games the sheikh would like to see (in any order). Each line consists of the two names of the teams which take part in that game. You can assume that it is always possible to find a tournament schedule consisting of the given games.
The last test case is followed by a zero.

Output Specification

For each test case, write the game schedule, distributed in rounds.
For each round, first write "Round #X" (where X is the round number) in a line by itself. Then write the games scheduled in this round in the form: "A defeats B", where A is the name of the advancing team and B is the name of the team being eliminated. You may write the games of a round in any order. If a wildcard is needed for the round, write "A advances with wildcard" after the last game of the round, where A is the name of the team which gets the wildcard. After the last round, write the winner in the format shown below. Print a blank line after each test case.

Sample Input


Sample Output

Round #1
B defeats A
C advances with wildcard
Round #2
C defeats B
Winner: C

Round #1
A defeats B
C defeats D
E advances with wildcard
Round #2
E defeats A
C advances with wildcard
Round #3
E defeats C
Winner: E

Note that there is always more than one possible game schedule; you may print any of them.

hide comments
abdou_93: 2013-08-21 18:56:00

why Wa ??! any help

Last edit: 2013-08-21 18:56:53
Adrian Kuegel: 2011-09-06 10:12:21

You may choose any team as the winner in the last match. Btw, in your code it seems that sometimes you forget to write the "advances with wildcard" lines even if they are required.

Sailesh Mukil: 2011-09-05 16:41:40

how do you decide the final winner in the tournament? Can we choose any one team as the winner in the last match?

:D: 2010-07-05 18:54:24

If You're having some problems check for extra whitespaces/tabs/empty lines. Probably they are not there, but I had some input issues in Perl.

Added by:Adrian Kuegel
Time limit:2.517s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL6 VB.NET
Resource:own problem, used in University of Ulm Local Contest 2005