Under construction

Still being built. Things may change

Tournament Manipulation

Time limit1sMemory limit128 MB

Summary
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 nn teams. In the first round there are n/2n/2 matches, and the winner of each match advances to the next round. The second round has n/4n/4 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 tt that Sunyoung's team cannot beat, there always exists a team t′t' that beats tt 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 11) wins the tournament.

If several brackets let team 11 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 2020 of them.

The first line of each test case contains the number of teams nn (2≤n≤82 \le n \le 8, and nn is a power of two). Teams are numbered from 11 to nn, and Sunyoung's team is team 11.

The next nn lines each contain a binary string of length nn. If the kk-th character of the jj-th line is 1, then team jj can beat team kk; otherwise it is 0 (there are no draws). A team cannot play itself, so the jj-th character of the jj-th line is always 0. For distinct jj and kk, the kk-th character of the jj-th line and the jj-th character of the kk-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 n−1n-1 lines.

The first n/2n/2 lines are the matches of the first round, the next n/4n/4 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.

Examples3

  1. Example 1

    Input
    4
    0110
    0011
    0000
    1010
    8
    00111010
    10101111
    00010010
    01000101
    00110010
    10101011
    00010000
    10101010
    
    Expected output
    1 3
    2 4
    1 2
    1 3
    2 4
    5 7
    6 8
    1 5
    4 6
    1 4
    
  2. Example 2

    Input
    2
    01
    00
    
    Expected output
    1 2
    
  3. Example 3

    Input
    4
    0111
    0011
    0000
    0010
    
    Expected output
    1 2
    3 4
    1 4