Tournament Rank Range
Time limit1sMemory limit128 MB
Given a single-elimination bracket's match winners, determine each queried player's best possible and worst possible final ranking consistent with the known beat relations.
- Level
Medium6 of 10
- Topics
- Tree, DFS, Combinatorics
- Solved
- No attempts yet
Problem
There are 2^N players in a single-elimination tournament. The players are numbered from 1 to 2^N. In the first round, player 2k-1 plays against player 2k, the loser of each match is eliminated, and the winner advances. Matches continue until only one player remains.
The tournament result can be represented as a full binary tree. Every non-leaf node stores the winner of that match. If player A defeated player B, and player B defeated player C, then A is also considered to have defeated C.
The champion's rank is fixed as rank 1. Other players may be able to claim several different ranks as long as the known win relation is not contradicted. Given the tournament result and several queried players, find the highest and lowest rank each queried player can possibly have.
Input
The first line contains the number of test cases T.
Each test case consists of three lines.
The first line contains N. (1 <= N <= 7)
The second line contains 2^N - 1 integers describing the tournament result. They are listed from the first round to the final round, and within each round from the leftmost match to the rightmost match. Each integer is the winner number of that match.
The third line contains the number of queries M followed by player numbers p_1, p_2, ..., p_M, separated by spaces.
Output
For each queried player p_i, print one line in the following format.
Player p_i can be ranked as high as h or as low as l.
Here h is the highest possible rank and l is the lowest possible rank. Print the answers in the same order as the queries. Print one blank line between the outputs of consecutive test cases.