Death Star
Time limit1sMemory limit256 MB
Recover the lexicographically smallest sequence whose pairwise bitwise AND values match the given off-diagonal matrix.
- Level
Medium4 of 10
- Topics
- Bit manipulation
- Solved
- No attempts yet
Problem
Ivan, a young Jedi, has been sent to infiltrate the Death Star and destroy it. Destroying it takes a sequence of non-negative integers , and Ivan does not have that sequence. What he has is a note from his old friend Darth Vader, and the note states the condition the sequence must satisfy.
The note holds a square matrix of size . The value in row and column is the bitwise and of and . A lightsaber damaged the note, so the main diagonal is unreadable, and the input gives it as 0.
Several sequences can satisfy the condition, and at least one always exists. Recover the one that comes first in lexicographic order.
Input
The first line has the size of the matrix ().
Each of the next lines has one row of the matrix: integers () separated by single spaces. Every diagonal value is 0, and holds whenever . At least one valid sequence exists, and every element of it is at most .
Output
Print on one line the lexicographically smallest sequence that satisfies the condition, as integers separated by single spaces.
Lexicographic comparison reads two sequences from the first element and takes the one with the smaller value at the first position where they differ.