第 228 场 AtCoder Regular Contest++
D
Amidakuji 2
AtCoder
⌀ 3506
原题
← CE →

Amidakuji 2AI (CI翻译)

Problem Statement

For a permutation $Q$ of $(1,2,\dots,N)$ and a non-negative integer $k$, define a permutation $Q^k$ of $(1,2,\dots,N)$ as follows.

  • Let $Q^k$ be the sequence $R=(1,2,\dots,N)$ obtained after performing the following operation $k$ times.
  • For $i = 1,2,\dots,N$, replace $R_i$ with $Q_{R_i}$.

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

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

  • For every integer $i$ satisfying $1 \le i \le M$, there exists a non-negative integer $k$ such that $P_i = Q^k$.

You are given $T$ test cases; solve each of them.

Constraints

  • $1 \le T \le 250000$
  • $1 \le N,M \le 500$
  • $P_1,P_2,\dots,P_M$ are permutations of $(1,2,\dots,N)$.
  • The sum of $NM$ over all test cases is at most $250000$.
  • All input values are integers.

Input

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

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N M
P_{1,1}\ P_{1,2}\ \dots\ P_{1,N}
P_{2,1}\ P_{2,2}\ \dots\ P_{2,N}
\vdots
P_{M,1}\ P_{M,2}\ \dots\ P_{M,N}

Output

Output $T$ lines. The $i$-th line should contain the answer to $\mathrm{case}_i$ in the following format.

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

Q_1\ Q_2\ \dots\ Q_N

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

{"titleZh":"阿弥陀签 2","bodyZh":"## 题目描述\n\n对于 $(1,2,\\dots,N)$ 的一个排列 $Q$ 和一个非负整数 $k$,定义 $(1,2,\\dots,N)$ 的一个排列 $Q^k$,具体如下。\n\n- 令 $Q^k$ 为序列 $R=(1,2,\\dots,N)$ 执行以下操作 $k$ 次后得到的结果。\n- 对于 $i = 1,2,\\dots,N$,将 $R_i$ 替换为 $Q_{R_i}$。\n\n给定 $(1,2,\\dots,N)$ 的 $M$ 个排列。第 $i$ 个排列为 $P_i = (P_{i,1},P_{i,2},\\dots,P_{i,N})$。\n\n请判断是否存在一个满足以下条件的 $(1,2,\\dots,N)$ 的排列 $Q=(Q_1,Q_2,\\dots,Q_N)$,如果存在,请找出其中一个。\n\n- 对于满足 $1 \\le i \\le M$ 的每个整数 $i$,都存在一个非负整数 $k$,使得 $P_i = Q^k$。\n\n给定 $T$ 组测试用例,请分别求解每组测试用例。\n\n## 约束条件\n\n- $1 \\le T \\le 250000$\n- $1 \\le N,M \\le 500$\n- $P_1,P_2,\\dots,P_M$ 均为 $(1,2,\\dots,N)$ 的排列。\n- 所有测试用例的 $NM$ 之和不超过 $250000$。\n- 所有输入值均为整数。\n\n## 输入格式\n\n输入由标准输入给出,格式如下:\n\n\nT\n\\mathrm{case}_1\n\\mathrm{case}_2\n\\vdots\n\\mathrm{case}_T\n`\n\n每组测试用例的格式如下:\n\n`\nN M\nP_{1,1}\\ P_{1,2}\\ \\dots\\ P_{1,N}\nP_{2,1}\\ P_{2,2}\\ \\dots\\ P_{2,N}\n\\vdots\nP_{M,1}\\ P_{M,2}\\ \\dots\\ P_{M,N}\n`\n\n## 输出格式\n\n输出 $T$ 行。第 $i$ 行按照以下格式输出 $\mathrm{case}_i$ 的答案。\n\n如果不存在满足条件的 $Q$,输出 -1。如果存在,则按照以下格式输出其中一个:\n\n`\nQ_1\\ Q_2\\ \\dots\\ Q_N\n\n\n如果有多个满足条件的 $Q$,输出其中任意一个均可通过评测。"}AI (CI翻译)

样例

Input #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
Output #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
For the first case, for example, $Q=(3,1,2)$ satisfies the condition. Since $Q^0 = (1,2,3),Q^1 = (3,1,2),Q^2 = (2,3,1)$, we have $P_1 = Q^2,P_2 = Q^1$, so the condition is satisfied. For the second case, there is no $Q$ satisfying the condition.
题目信息
题号
arc228_d
时间限制
4 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。