Knockout Tournament

Time limit1sMemory limit128 MB

Summary
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 2n2^n 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 1,2,3,…,2n1, 2, 3, \ldots, 2^n, and let the first-round pairings be player 2k−12k-1 versus player 2k2k for k=1,2,…,2n−1k = 1, 2, \ldots, 2^{n-1}. 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 n=3n = 3. In round 1 the winner of (1,2)(1,2) is 1, of (3,4)(3,4) is 3, of (5,6)(5,6) is 5, and of (7,8)(7,8) is 8. In round 2 the winner of (1,3)(1,3) is 1 and of (5,8)(5,8) is 8. The final (1,8)(1,8) 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 nn (n<8n < 8), meaning there are 2n2^n players numbered 11 through 2n2^n and paired as described above. A value of n=0n = 0 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 2n−12^{n-1} winners of round 1, then the 2n−22^{n-2} winners of round 2, …\ldots, and finally the single winner of the last round, for a total of 2n−12^n - 1 winner numbers. For example, the n=3n = 3 tournament above is given by

1 3 5 8 1 8 1

The third line contains a positive integer mm followed by mm integers k1,…,kmk_1, \ldots, k_m, where each kik_i is a player in the tournament.

Output

For each kik_i, 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 kik_i appear in the input. Separate the output of different test cases with a blank line.

Examples3

  1. Example 1

    Input
    3
    1 3 5 8 1 8 1
    2 2 5
    4
    2 3 6 7 9 11 14 15 3 6 9 15 6 9 6
    4 1 15 7 6
    0
    
    Expected output
    Player 2 can be ranked as high as 2 or as low as 8.
    Player 5 can be ranked as high as 3 or as low as 7.
    
    Player 1 can be ranked as high as 4 or as low as 16.
    Player 15 can be ranked as high as 3 or as low as 13.
    Player 7 can be ranked as high as 2 or as low as 15.
    Player 6 can be ranked as high as 1 or as low as 1.
    
  2. Example 2

    Input
    1
    1
    2 1 2
    0
    
    Expected output
    Player 1 can be ranked as high as 1 or as low as 1.
    Player 2 can be ranked as high as 2 or as low as 2.
    
  3. Example 3

    Input
    2
    1 3 1
    4 1 2 3 4
    0
    
    Expected output
    Player 1 can be ranked as high as 1 or as low as 1.
    Player 2 can be ranked as high as 2 or as low as 4.
    Player 3 can be ranked as high as 2 or as low as 3.
    Player 4 can be ranked as high as 3 or as low as 4.