CF1500C.Matrix Sorting

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given two tables AA and BB of size n×mn \times m .

We define a sorting by column as the following: we choose a column and reorder the rows of the table by the value in this column, from the rows with the smallest value to the rows with the largest. In case there are two or more rows with equal value in this column, their relative order does not change (such sorting algorithms are called stable).

You can find this behavior of sorting by column in many office software for managing spreadsheets. Petya works in one, and he has a table AA opened right now. He wants to perform zero of more sortings by column to transform this table to table BB .

Determine if it is possible to do so, and if yes, find a sequence of columns to sort by. Note that you do not need to minimize the number of sortings.

输入格式

The first line contains two integers nn and mm ( 1n,m15001 \le n, m \le 1500 ) — the sizes of the tables.

Each of the next nn lines contains mm integers ai,ja_{i,j} ( 1ai,jn1 \le a_{i, j} \le n ), denoting the elements of the table AA .

Each of the next nn lines contains mm integers bi,jb_{i, j} ( 1bi,jn1 \le b_{i, j} \le n ), denoting the elements of the table BB .

输出格式

If it is not possible to transform AA into BB , print 1-1 .

Otherwise, first print an integer kk ( 0k50000 \le k \le 5000 ) — the number of sortings in your solution.

Then print kk integers c1,,ckc_1, \ldots, c_k ( 1cim1 \le c_i \le m ) — the columns, by which Petya needs to perform a sorting.

We can show that if a solution exists, there is one in no more than 50005000 sortings.

输入输出样例

  • 输入#1

    2 2
    2 2
    1 2
    1 2
    2 2

    输出#1

    1
    1
  • 输入#2

    3 3
    2 3 2
    1 3 3
    1 1 2
    1 1 2
    1 3 3
    2 3 2

    输出#2

    2
    1 2
  • 输入#3

    2 2
    1 1
    2 1
    2 1
    1 1

    输出#3

    -1
  • 输入#4

    4 1
    2
    2
    2
    1
    1
    2
    2
    2

    输出#4

    1
    1

说明/提示

Consider the second example. After the sorting by the first column the table becomes

$$\begin{matrix} 1&3&3\\ 1&1&2\\ 2&3&2. \end{matrix} $$ </p><p>After the sorting by the second column the table becomes</p><p> $$ \begin{matrix} 1&1&2\\ 1&3&3\\ 2&3&2, \end{matrix} $$

and this is what we need.

In the third test any sorting does not change anything, because the columns are already sorted.

首页