Single-Crossing

시간 제한3초메모리 제한2048 MB

요약
크기 m인 순열 n개가 주어질 때, 임의의 두 값이 상대 순서를 최대 한 번만 바꾸도록 순열들을 재배열할 수 있는지 판정하고 그 순서를 출력한다.
난이도

어려움10점 중 8점

유형
그래프, 위상 정렬, 정렬, 그리디
정답자
아직 제출이 없습니다

문제

The summer has already been long and boring, and to entertain yourself, you started to look over some recent papers. You stumbled upon an interesting problem: Let's consider a list of nn permutations X1,…,XnX^1, \ldots, X^n over 1,2,…,m\\{1, 2, \ldots, m\\}. In other words, each XiX^i is a vector Xi_1,…,Xi_mX^i\_1, \ldots, X^i\_m of size mm in which all the numbers from 11 to mm appear exactly once. The paper is about rearranging the given permutations such that the new order, let it be Y1,…,YnY^1, \ldots, Y^n, is single-crossing.

A sequence of permutations Y1,…,YnY^1, \ldots, Y^n is called single-crossing if and only if, when we choose any three indices 1≤i<j<k≤n1 \leq i < j < k \leq n and any two distinct values 1≤a,b≤m1 \leq a, b \leq m such that aa appears before bb in both YiY^i and YkY^k, it holds that aa appears before bb in YjY^j as well.

In a more intuitive way: we say that Y1,…,YnY^1, \ldots, Y^n is single-crossing if and only if any two elements aa and bb change their relative order at most once (see the image above).

You can't find the paper anymore, but you really want to implement a solution for the problem it proposes. So, given tt test cases, find out for each of them if there is such a way to rearrange the permutations to be single-crossing, and, if so, output one possible solution.

입력

The first line contains one number tt (1≤t≤51 \leq t \leq 5), the number of test cases.

Each test case is described as follows. The first line contains two integers nn and mm (1≤n≤1051 \leq n \leq 10^5; 1≤n⋅m≤1061 \leq n \cdot m \leq 10^6). Each of the next nn lines contains mm integers: the permutations X1,…,XnX^1, \ldots, X^n.

출력

For each of the tt test cases, print a single line. If there is no way to rearrange the permutations so that the sequence becomes single-crossing, print -1. Otherwise, print a permutation pp containing nn space-separated integers: the order in which the original permutations could be rearranged.

If there are multiple solutions, output any one of them.

힌트

first test case,
ordered as 2 3 1 5 4:
1 2 3 4
2 1 3 4
2 3 1 4
3 2 4 1
4 3 2 1

예제1

  1. 예제 1

    입력
    2
    5 4
    2 3 1 4
    1 2 3 4
    2 1 3 4
    4 3 2 1
    3 2 4 1
    4 4
    2 1 3 4
    1 2 3 4
    2 1 4 3
    1 2 4 3
    
    예상 출력
    2 3 1 5 4
    -1