第 1120 场 Codeforces Round(Div. 2)
F
PLUS世界
PLUSworld
Codeforces
未评级
原题
← E→

PLUS世界AI (CI翻译)

Farmer John is building PLUSworld, a giant barn complex with $n$ barns numbered from $1$ to $n$. For each $1 \le i \le n$, there is currently a one-way walkway from barn $i$ to barn $a_i$.

Farmer John 正在建造 PLUSworld,这是一个由 $n$ 个谷仓组成的大型建筑群,谷仓编号为 $1$ 到 $n$。对于每个 $1 \le i \le n$,当前都有一条从谷仓 $i$ 通往谷仓 $a_i$ 的单向道路。AI (CI翻译)

Before opening PLUSworld, Farmer John wants to reconfigure the roads so that, for every $1 \le i \le n$, the road from barn $i$ points to barn $b_i$. The target roads have the special property that each road points to its own barn or to a higher-numbered barn $(i \le b_i)$.

在开放 PLUSworld 之前,Farmer John 想要重新规划道路,使得对于每个 $1 \le i \le n$,从谷仓 $i$ 出发的道路通往谷仓 $b_i$。目标道路具有一个特殊性质:每条道路都通往自身所在的谷仓或编号更大的谷仓 $(i \le b_i)$。AI (CI翻译)

To do this, he sends Bessie to one barn of his choice. Let $p$ be the barn where Bessie is currently located. Bessie may perform the following operations any number of times:

为此,他让 Bessie 前往一个由他选择的谷仓。设 Bessie 当前所在的谷仓为 $p$。Bessie 可以执行任意次以下操作:AI (CI翻译)

  • Operation $1$: Increase the destination of the road from the current barn by $1$. More formally, set $a_p := a_p + 1$. This operation may only be performed if $a_p \lt n$.
  • Operation $2$: Follow the road from the current barn. More formally, set $p := a_p$.
  • 操作 $1$:将当前谷仓的道路目的地编号加 $1$。更形式化地,令 $a_p := a_p + 1$。仅当 $a_p \lt n$ 时才能执行此操作。
  • 操作 $2$:沿着当前谷仓的道路前进。更形式化地,令 $p := a_p$。
AI (CI翻译)

Operation $1$ changes the array $a$, but Bessie stays at the same barn. Operation $2$ changes Bessie's current barn, but does not change the array.

操作 $1$ 会改变数组 $a$,但 Bessie 仍留在当前谷仓。操作 $2$ 会改变 Bessie 当前所在的谷仓,但不会改变数组。AI (CI翻译)

Your goal is to make the array $a$ equal to the array $b$.

你的目标是使数组 $a$ 与数组 $b$ 相等。AI (CI翻译)

Determine whether this is possible. If it is possible, output any valid starting barn and any sequence of operations that makes $a=b$.

判断是否可能做到。如果可能,输出任意一个有效的起始谷仓,以及一串能够使 $a=b$ 的操作序列。AI (CI翻译)

It can be shown that if a solution exists, then there exists one using at most $2n^2$ operations.

可以证明,如果存在解,那么一定存在一个至多包含 $2n^2$ 次操作的解。AI (CI翻译)

Input

输入格式

AI (CI翻译)

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

每个输入包含多组测试用例。第一行包含测试用例数 $t$($1 \le t \le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)

The first line of each test case contains a single integer $n$ ($1\le n\le 1000$).

每个测试用例的第一行包含一个整数 $n$($1\le n\le 1000$)。AI (CI翻译)

The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le n$).

第二行包含 $n$ 个整数 $a_1,a_2,\ldots,a_n$($1\le a_i\le n$)。AI (CI翻译)

The third line contains $n$ integers $b_1,b_2,\ldots,b_n$ ($\color{red}{i\le b_i}\le n$).

第三行包含 $n$ 个整数 $b_1,b_2,\ldots,b_n$($\color{red}{i\le b_i}\le n$)。AI (CI翻译)

It is guaranteed that the sum of $n^2$ over all test cases does not exceed $1000^2$.

保证所有测试用例的 $n^2$ 之和不超过 $1000^2$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, if it is impossible to make $a$ equal to $b$, output a single integer $-1$.

对于每个测试用例,如果无法使 $a$ 与 $b$ 相等,输出一个整数 $-1$。AI (CI翻译)

Otherwise, output two lines.

否则,输出两行。AI (CI翻译)

On the first line, output two integers $o$ and $p$ — the number of operations you will perform and the initial index you choose ($0 \leq o \leq 2n^2$, $1 \leq p \leq n$).

第一行输出两个整数 $o$ 和 $p$,分别表示操作次数和选择的初始谷仓编号($0 \leq o \leq 2n^2$,$1 \leq p \leq n$)。AI (CI翻译)

On the second line, output $o$ integers $c_1,c_2,\ldots,c_o$ ($1\le c_i\le 2$), where $c_i$ is the type of the $i$-th operation.

第二行输出 $o$ 个整数 $c_1,c_2,\ldots,c_o$($1\le c_i\le 2$),其中 $c_i$ 表示第 $i$ 次操作的类型。AI (CI翻译)

If $c_i=1$, you perform operation $1$ and set $a_p:=a_p+1$. This operation may only be used if $a_p \lt n$ at that moment.

如果 $c_i=1$,则执行操作 $1$,令 $a_p:=a_p+1$。在执行该操作时,必须满足 $a_p \lt n$。AI (CI翻译)

If $c_i=2$, you perform operation $2$ and set $p:=a_p$.

如果 $c_i=2$,则执行操作 $2$,令 $p:=a_p$。AI (CI翻译)

After performing all $o$ operations, the array $a$ must be equal to $b$.

执行完全部 $o$ 次操作后,数组 $a$ 必须与数组 $b$ 相等。AI (CI翻译)

If there are multiple valid answers, you may output any of them.

如果有多个有效答案,你可以输出任意一个。AI (CI翻译)

Note

说明

AI (CI翻译)

For the first test case, Bessie starts at barn $1$. She increases $a_1$ from $1$ to $2$, follows the road to barn $2$, and increases $a_2$ from $2$ to $3$. The resulting array is $[2,3,3]$, which is equal to $b$.

对于第一个测试用例,Bessie 从谷仓 $1$ 出发。她将 $a_1$ 从 $1$ 增加到 $2$,沿道路前往谷仓 $2$,再将 $a_2$ 从 $2$ 增加到 $3$。最终数组为 $[2,3,3]$,与 $b$ 相等。AI (CI翻译)

For the second test case, $a_1=2 \gt b_1=1$. Since road destinations can only be increased, it is impossible to make $a_1$ equal to $b_1$, so the answer is $-1$.

对于第二个测试用例,$a_1=2 \gt b_1=1$。由于道路目的地编号只能增加,因此无法使 $a_1$ 等于 $b_1$,答案为 $-1$。AI (CI翻译)

For the third test case, Bessie starts at barn $1$. She changes $a_1$ from $1$ to $2$ and follows the road to barn $2$. She then changes $a_2$ from $1$ to $3$ and moves to barn $3$, changes $a_3$ from $1$ to $4$ and moves to barn $4$, and finally changes $a_4$ from $4$ to $5$. The resulting array is $[2,3,4,5,5]$, which is equal to $b$.

对于第三个测试用例,Bessie 从谷仓 $1$ 出发。她将 $a_1$ 从 $1$ 改为 $2$,并沿道路前往谷仓 $2$。接着,她将 $a_2$ 从 $1$ 改为 $3$,并前往谷仓 $3$;将 $a_3$ 从 $1$ 改为 $4$,并前往谷仓 $4$;最后将 $a_4$ 从 $4$ 改为 $5$。最终数组为 $[2,3,4,5,5]$,与 $b$ 相等。AI (CI翻译)

样例

Input #1
9
3
1 2 3
2 3 3
2
2 2
1 2
5
1 1 1 4 5
2 3 4 5 5
3
1 1 1
1 2 3
3
1 1 1
3 2 3
3
1 1 3
1 2 3
4
1 1 1 4
3 3 4 4
4
1 4 3 4
1 4 3 4
5
1 3 4 5 4
2 3 4 5 5
Output #1
5 1
1 2 1 2 2
-1
12 1
1 2 1 1 2 1 1 1 2 1 2 2
-1
-1
2 2
1 2
12 1
1 1 2 1 2 1 1 2 1 1 2 2
0 1
7 1
1 2 2 2 2 1 2
InputCopy OutputCopy
题目信息
题号
2263F
时间限制
2 s
内存限制
256 MB

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