Single-Crossing
시간 제한3초메모리 제한2048 MB
크기 m인 순열 n개가 주어질 때, 임의의 두 값이 상대 순서를 최대 한 번만 바꾸도록 순열들을 재배열할 수 있는지 판정하고 그 순서를 출력한다.
문제
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 permutations over . In other words, each is a vector of size in which all the numbers from to appear exactly once. The paper is about rearranging the given permutations such that the new order, let it be , is single-crossing.
A sequence of permutations is called single-crossing if and only if, when we choose any three indices and any two distinct values such that appears before in both and , it holds that appears before in as well.
In a more intuitive way: we say that is single-crossing if and only if any two elements and 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 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 (), the number of test cases.
Each test case is described as follows. The first line contains two integers and (; ). Each of the next lines contains integers: the permutations .
출력
For each of the 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 containing 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