Tournament Manipulation
Time limit1sMemory limit128 MB
Given head-to-head results for n teams (n up to 8) with a guaranteed property for team 1, construct the lexicographically smallest single-elimination bracket where team 1 becomes champion.
- Level
Hard8 of 10
- Topics
- Backtracking, Greedy, Brute force
- Solved
- No attempts yet
Problem
A soccer tournament has teams. In the first round there are matches, and the winner of each match advances to the next round. The second round has matches, and this repeats until the final match between the last two teams. The winner of the final becomes the tournament champion.
Sunyoung owns a soccer team. It is not the strongest team in the world, but it is quite strong: it can surely beat at least half of the teams in the tournament. Moreover, for every team that Sunyoung's team cannot beat, there always exists a team that beats while itself losing to Sunyoung's team.
You may arrange the bracket however you like: in each round you may pair up the teams that reached that round into matches in any way you want. The outcome of a match is already fixed by the head-to-head record of the two teams and cannot be changed, but you decide who plays whom. Arrange the bracket so that Sunyoung's team (team ) wins the tournament.
If several brackets let team win, output only the lexicographically smallest one. The ordering is defined in the Output section.
Input
The input consists of several test cases; there are at most of them.
The first line of each test case contains the number of teams (, and is a power of two). Teams are numbered from to , and Sunyoung's team is team .
The next lines each contain a binary string of length . If the -th character of the -th line is 1, then team can beat team ; otherwise it is 0 (there are no draws). A team cannot play itself, so the -th character of the -th line is always 0. For distinct and , the -th character of the -th line and the -th character of the -th line differ.
The input continues until end of file.
Output
For each test case, print a bracket that makes Sunyoung's team win, using lines.
The first lines are the matches of the first round, the next lines are the matches of the second round, and so on, with the last single line being the final. The teams appearing in the matches of a round must be exactly the teams that reached that round (the winners of the previous round), and each team plays exactly once per round.
Each line contains two integers, meaning those two teams play each other in that match.
Because several winning brackets may exist, output only the lexicographically smallest one under the following rules.
- When printing a match on a line, write the smaller of the two team numbers first.
- Within the same round, sort the matches in ascending order by first number, then by second number.
- To compare two brackets, sort and print them by the rules above, then read the integers top to bottom and left to right into one sequence and compare the sequences lexicographically. The bracket with the smaller sequence is the answer.
Print the outputs of the test cases one after another, with no blank line or separator between them.