Hamilton

시간 제한1초메모리 제한1024 MB

요약
대칭 0/1 행렬이 주어질 때, 순환 순서에서 간선 라벨이 많아야 한 번만 바뀌는 정점 순열을 찾는다.
난이도

어려움10점 중 8점

유형
그래프, 그리디, 구현, 수학
정답자
아직 제출이 없습니다

문제

Bobo has an n×nn \times n symmetric matrix CC consisting of zeros and ones. For a permutation p_1,…,p_np\_1, \dots, p\_n of 1,…,n1, \dots, n, let c_i={C_p_i,p_i+1for 1≤i<n C_p_n,p_1for i=n . c\_i = \begin{cases} C\_{p\_i, p\_{i + 1}} & \text{for } 1 \leq i < n \\\ C\_{p\_n, p\_1} & \text{for } i = n \\\ \end{cases}\text{.}

The permutation pp is almost monochromatic if and only if the number of indices ii (1≤i<n1 \leq i < n) where c_i≠c_i+1c\_i \neq c\_{i + 1} is at most one.

Find an almost monochromatic permutation p_1,…,p_np\_1, \dots, p\_n for the given matrix CC.

입력

The input consists of several test cases terminated by end-of-file. For each test case,

The first line contains an integer nn.

For the following nn lines, the ii-th line contains nn integers C_i,1,…,C_i,nC\_{i, 1}, \dots, C\_{i, n}.

출력

For each test case, if there exists an almost monochromatic permutation, output nn integers p_1,…,p_np\_1, \dots, p\_n which denote the permutation. Otherwise, output −1-1.

If there are multiple almost monochromatic permutations, any of them is considered correct.

제한

  • 3≤n≤20003 \le n \le 2000
  • C_i,j∈0,1C\_{i, j} \in \\{0, 1\\} for each 1≤i,j≤n1 \leq i, j \leq n
  • C_i,j=C_j,iC\_{i, j} = C\_{j, i} for each 1≤i,j≤n1 \leq i, j \leq n
  • C_i,i=0C\_{i, i} = 0 for each 1≤i≤n1 \leq i \leq n
  • In each input, the sum of nn does not exceed 20002000.

힌트

For the first test case, c_1=C_3,1=1c\_1 = C\_{3, 1} = 1, c_2=C_1,2=0c\_2 = C\_{1, 2} = 0, c_3=C_2,3=0c\_3 = C\_{2, 3} = 0. Only when i=1i = 1, c_i≠c_i+1c\_i \neq c\_{i + 1}. Therefore, the permutation 3,1,23, 1, 2 is an almost monochromatic permutation.

예제1

  1. 예제 1

    입력
    3
    001
    000
    100
    4
    0000
    0000
    0000
    0000
    
    예상 출력
    3 1 2
    2 4 3 1