AT_arc228_d.Amidakuji 2
NOI/NOI+/CTSC
通过率:0%
时间限制:4.00s
内存限制:1024MB
AC君温馨提醒
该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
For a permutation Q of (1,2,…,N) and a non-negative integer k, define a permutation Qk of (1,2,…,N) as follows.
- Let Qk be the sequence R=(1,2,…,N) obtained after performing the following operation k times.
- For i=1,2,…,N, replace Ri with QRi.
You are given M permutations of (1,2,…,N). The i-th permutation is Pi=(Pi,1,Pi,2,…,Pi,N).
Determine whether there exists a permutation Q=(Q1,Q2,…,QN) of (1,2,…,N) satisfying the following condition, and if it exists, find one such Q.
- For every integer i satisfying 1≤i≤M, there exists a non-negative integer k such that Pi=Qk.
You are given T test cases; solve each of them.
对于 (1,2,…,N) 的一个排列 Q 和一个非负整数 k,定义 (1,2,…,N) 的一个排列 Qk 如下:
- 令 Qk 为序列 R=(1,2,…,N) 经过以下操作 k 次后所得的结果:
- 对于 i=1,2,…,N,将 Ri 替换为 QRi。
给定 (1,2,…,N) 的 M 个排列。其中第 i 个排列为 Pi=(Pi,1,Pi,2,…,Pi,N)。
判断是否存在一个 (1,2,…,N) 的排列 Q=(Q1,Q2,…,QN),满足如下条件;若存在,输出任意一个满足条件的 Q:
- 对每个满足 1≤i≤M 的整数 i,均存在一个非负整数 k,使得 Pi=Qk。
你将得到 T 组测试数据,请对每组数据求解。
输入格式
The input is given from Standard Input in the following format:
T
case1
case2
⋮
caseT
Each test case is given in the following format:
N M
P1,1 P1,2 … P1,N
P2,1 P2,2 … P2,N
⋮
PM,1 PM,2 … PM,N
输入从标准输入中按以下格式给出:
T
case1
case2
⋮
caseT
每个测试用例按以下格式给出:
N M
P1,1 P1,2 … P1,N
P2,1 P2,2 … P2,N
⋮
PM,1 PM,2 … PM,N
输出格式
Output T lines. The i-th line should contain the answer to casei in the following format.
If there is no Q satisfying the condition, output -1. If one exists, output it in the following format:
Q1 Q2 … QN
If there are multiple Q satisfying the condition, any of them will be accepted.
输出 T 行。第 i 行应以如下格式给出 casei 的答案:
若不存在满足条件的 Q,则输出 -1;否则,按如下格式输出该 Q:
Q1 Q2 … QN
若存在多个满足条件的 Q,输出其中任意一个即可。
输入输出样例
输入#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) satisfies the condition.
Since Q0=(1,2,3),Q1=(3,1,2),Q2=(2,3,1), we have P1=Q2,P2=Q1, so the condition is satisfied.
For the second case, there is no Q satisfying the condition.
Constraints
- 1≤T≤250000
- 1≤N,M≤500
- P1,P2,…,PM are permutations of (1,2,…,N).
- The sum of NM over all test cases is at most 250000.
- All input values are integers.
样例 1 解释:
对于第一组测试数据,例如,Q=(3,1,2) 满足条件。
由于 Q0=(1,2,3), Q1=(3,1,2), Q2=(2,3,1),我们有 P1=Q2, P2=Q1,因此条件成立。
对于第二组测试数据,不存在满足条件的 Q。
约束条件
- 1≤T≤250000
- 1≤N,M≤500
- P1,P2,…,PM 均为 (1,2,…,N) 的排列。
- 所有测试用例中 NM 的总和不超过 250000。
- 所有输入值均为整数。
输入解题思路,AI测评打分。不知道怎么写?