Under construction

Still being built. Things may change

King of the Waves

Time limit2sMemory limit512 MB

Summary
Decide whether Henk (participant 0) can be made tournament king, and if so print the specified BFS-tree order reversed.
Level

Medium5 of 10

Topics
BFS, Graph, Greedy
Solved
No attempts yet

Problem

You run a king of the hill tournament with nn participants, the Buenos Aires Paddleboarding Competition (BAPC). One participant starts as king and the others challenge the king one at a time. The winner of a match becomes the new king. Every participant except the starting one challenges exactly once, and a paddleboarding match never ends in a draw. Whoever is king after the last match wins the tournament.

You are the organiser, so you pick who starts as king and the order of the challenges. Someone offers you a large sum of money if participant 00, Henk, wins the tournament. You happen to know, for any two participants xx and yy, which of them would win their match. So you decide to rig the game. Decide whether a schedule that makes Henk win exists, and if it does, print the schedule defined in the output section.

Input

The first line contains an integer nn (1≤n≤10001 \le n \le 1000), the number of participants. The participants are numbered 00 to n−1n-1, and Henk is participant 00.

Each of the next nn lines contains exactly nn characters. The jj-th character of the ii-th line describes the match between participant ii and participant jj (0≤i,j<n0 \le i, j < n):

  • 1 if participant ii beats participant jj,
  • 0 if participant ii loses to participant jj,
  • X if i=ji = j.

Output

If no schedule makes Henk win, print impossible.

Otherwise print the single schedule defined by the rule below.

Build a tree rooted at participant 00 with a breadth first search. Mark participant 00 as visited and put it in a queue. While the queue is not empty, take the participant uu at the front of the queue and look at j=0,1,…,n−1j = 0, 1, \dots, n-1 in increasing order. Whenever jj is not visited yet and uu beats jj, mark jj as visited, record uu as the parent of jj, and push jj to the back of the queue. A schedule that makes Henk win exists if and only if this search visits every participant.

Traverse that tree in preorder, visiting a participant before its children and visiting the children of a participant in increasing order of index. Print the traversal reversed on one line, with the numbers separated by single spaces. The participant printed first starts as king, and the others challenge the king in the printed order.

Whenever some schedule makes Henk win, this schedule makes him win too. Other winning schedules can exist, and only this one is accepted.

Examples2

  1. Example 1

    Input
    3
    X10
    0X1
    10X
    
    Expected output
    2 1 0
    
  2. Example 2

    Input
    3
    X10
    0X0
    11X
    
    Expected output
    impossible