Knockout Tournament
Time limit1sMemory limit128 MB
Given knockout tournament results, find the best and worst possible rank each queried player could hold under a transitive beat relation.
- Level
Medium6 of 10
- Topics
- Tree, DFS, Implementation, Graph
- Solved
- No attempts yet
Problem
There is a knockout (single-elimination) tournament with players. A single loss eliminates a player; the winners then play each other, and the winners of those matches advance, until only one player remains.
Number the players , and let the first-round pairings be player versus player for . Then the whole tournament can be described by a complete binary tree in which each interior node holds the winner of that match.
For example, take the tournament with . In round 1 the winner of is 1, of is 3, of is 5, and of is 8. In round 2 the winner of is 1 and of is 8. The final is won by 1, so player 1 is the champion.
After the tournament, some reporters argued about the relative ranking of the players implied by the results. Assume that winning is transitive: if player A beats player B and player B beats player C, then player A also beats player C. With that assumption there is no doubt about who the best player is.
The question is: based only on the tournament results, what is the highest ranking a player can reasonably claim, and what is the lowest (worst) ranking a player can have? For example, in the tournament above player 2, having lost only to the eventual champion, could claim to be 2nd best, yet could really be the worst (ranked 8th). Player 5 could claim to be as high as 3rd (having lost to someone who could be 2nd) but no worse than 7th (having beaten one player in round 1).
Given the tournament results and a list of players of interest, determine the highest and lowest possible ranking of each of those players.
Input
The input contains several test cases. Each test case consists of three lines.
The first line contains a positive integer (), meaning there are players numbered through and paired as described above. A value of marks the end of the input.
The second line lists the match results round by round, starting with round 1 and, within each round, from left to right: the winners of round 1, then the winners of round 2, , and finally the single winner of the last round, for a total of winner numbers. For example, the tournament above is given by
1 3 5 8 1 8 1
The third line contains a positive integer followed by integers , where each is a player in the tournament.
Output
For each , print one line of the form
Player ki can be ranked as high as h or as low as l.
where ki is the player number, h is the highest ranking that player can claim, and l is the lowest ranking that player can have, each replaced by the appropriate number. The lines must appear in the same order as the appear in the input. Separate the output of different test cases with a blank line.