AT_arc228_d.Amidakuji 2

NOI/NOI+/CTSC

通过率:0%

时间限制:4.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

For a permutation QQ of (1,2,…,N)(1,2,\dots,N) and a non-negative integer kk, define a permutation QkQ^k of (1,2,…,N)(1,2,\dots,N) as follows.

  • Let QkQ^k be the sequence R=(1,2,…,N)R=(1,2,\dots,N) obtained after performing the following operation kk times.
    • For i=1,2,…,Ni = 1,2,\dots,N, replace RiR_i with QRiQ_{R_i}.

You are given MM permutations of (1,2,…,N)(1,2,\dots,N). The ii-th permutation is Pi=(Pi,1,Pi,2,…,Pi,N)P_i = (P_{i,1},P_{i,2},\dots,P_{i,N}).

Determine whether there exists a permutation Q=(Q1,Q2,…,QN)Q=(Q_1,Q_2,\dots,Q_N) of (1,2,…,N)(1,2,\dots,N) satisfying the following condition, and if it exists, find one such QQ.

  • For every integer ii satisfying 1≤i≤M1 \le i \le M, there exists a non-negative integer kk such that Pi=QkP_i = Q^k.

You are given TT test cases; solve each of them.

对于 (1,2,…,N)(1,2,\dots,N) 的一个排列 QQ 和一个非负整数 kk,定义 (1,2,…,N)(1,2,\dots,N) 的一个排列 QkQ^k 如下:

  • 令 QkQ^k 为序列 R=(1,2,…,N)R=(1,2,\dots,N) 经过以下操作 kk 次后所得的结果:
    • 对于 i=1,2,…,Ni = 1,2,\dots,N,将 RiR_i 替换为 QRiQ_{R_i}。

给定 (1,2,…,N)(1,2,\dots,N) 的 MM 个排列。其中第 ii 个排列为 Pi=(Pi,1,Pi,2,…,Pi,N)P_i = (P_{i,1},P_{i,2},\dots,P_{i,N})。

判断是否存在一个 (1,2,…,N)(1,2,\dots,N) 的排列 Q=(Q1,Q2,…,QN)Q=(Q_1,Q_2,\dots,Q_N),满足如下条件;若存在,输出任意一个满足条件的 QQ:

  • 对每个满足 1≤i≤M1 \le i \le M 的整数 ii,均存在一个非负整数 kk,使得 Pi=QkP_i = Q^k。

你将得到 TT 组测试数据,请对每组数据求解。

输入格式

The input is given from Standard Input in the following format:

TT
case1\mathrm{case}_1
case2\mathrm{case}_2
⋮\vdots
caseT\mathrm{case}_T

Each test case is given in the following format:

NN MM
P1,1 P1,2 … P1,NP_{1,1}\ P_{1,2}\ \dots\ P_{1,N}
P2,1 P2,2 … P2,NP_{2,1}\ P_{2,2}\ \dots\ P_{2,N}
⋮\vdots
PM,1 PM,2 … PM,NP_{M,1}\ P_{M,2}\ \dots\ P_{M,N}

输入从标准输入中按以下格式给出:

TT
case1\mathrm{case}_1
case2\mathrm{case}_2
⋮\vdots
caseT\mathrm{case}_T

每个测试用例按以下格式给出:

NN MM
P1,1 P1,2 … P1,NP_{1,1}\ P_{1,2}\ \dots\ P_{1,N}
P2,1 P2,2 … P2,NP_{2,1}\ P_{2,2}\ \dots\ P_{2,N}
⋮\vdots
PM,1 PM,2 … PM,NP_{M,1}\ P_{M,2}\ \dots\ P_{M,N}

输出格式

Output TT lines. The ii-th line should contain the answer to casei\mathrm{case}_i in the following format.

If there is no QQ satisfying the condition, output -1. If one exists, output it in the following format:

Q1 Q2 … QNQ_1\ Q_2\ \dots\ Q_N

If there are multiple QQ satisfying the condition, any of them will be accepted.

输出 TT 行。第 ii 行应以如下格式给出 casei\mathrm{case}_i 的答案:

若不存在满足条件的 QQ,则输出 -1;否则,按如下格式输出该 QQ:

Q1 Q2 … QNQ_1\ Q_2\ \dots\ Q_N

若存在多个满足条件的 QQ,输出其中任意一个即可。

输入输出样例

  • 输入#1

    6
    3 2
    2 3 1
    3 1 2
    3 2
    2 3 1
    1 3 2
    5 2
    1 5 2 4 3
    4 2 3 1 5
    5 1
    4 2 5 1 3
    9 4
    2 9 7 5 4 3 8 1 6
    3 7 2 5 4 1 9 6 8
    3 7 2 4 5 1 9 6 8
    7 8 9 4 5 2 6 3 1
    10 4
    9 2 1 4 5 6 8 3 7 10
    7 4 9 5 10 2 3 1 8 6
    3 4 8 5 10 2 9 7 1 6
    1 10 3 6 2 5 7 8 9 4

    输出#1

    3 1 2
    -1
    4 5 2 1 3
    4 2 5 1 3
    7 8 9 5 4 2 6 3 1
    -1

说明/提示

Sample 1 Explanation:
For the first case, for example, Q=(3,1,2)Q=(3,1,2) satisfies the condition.

Since Q0=(1,2,3),Q1=(3,1,2),Q2=(2,3,1)Q^0 = (1,2,3),Q^1 = (3,1,2),Q^2 = (2,3,1), we have P1=Q2,P2=Q1P_1 = Q^2,P_2 = Q^1, so the condition is satisfied.

For the second case, there is no QQ satisfying the condition.

Constraints

  • 1≤T≤2500001 \le T \le 250000
  • 1≤N,M≤5001 \le N,M \le 500
  • P1,P2,…,PMP_1,P_2,\dots,P_M are permutations of (1,2,…,N)(1,2,\dots,N).
  • The sum of NMNM over all test cases is at most 250000250000.
  • All input values are integers.

样例 1 解释:
对于第一组测试数据,例如,Q=(3,1,2)Q=(3,1,2) 满足条件。

由于 Q0=(1,2,3), Q1=(3,1,2), Q2=(2,3,1)Q^0 = (1,2,3),\ Q^1 = (3,1,2),\ Q^2 = (2,3,1),我们有 P1=Q2, P2=Q1P_1 = Q^2,\ P_2 = Q^1,因此条件成立。

对于第二组测试数据,不存在满足条件的 QQ。

约束条件

  • 1≤T≤2500001 \le T \le 250000
  • 1≤N,M≤5001 \le N,M \le 500
  • P1,P2,…,PMP_1,P_2,\dots,P_M 均为 (1,2,…,N)(1,2,\dots,N) 的排列。
  • 所有测试用例中 NMNM 的总和不超过 250000250000。
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页