#CF2237E. 置换交换 / E. Permutation Commutation

    ID: 18579 传统题 2000ms 512MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>CodeforcesOrder Capital Round 2 (Codeforces Round 1104, Div. 1 + Div. 2)

置换交换 / E. Permutation Commutation

E. Permutation Commutation

Source: Codeforces 2237E
Contest: Order Capital Round 2 (Codeforces Round 1104, Div. 1 + Div. 2)
Time limit: 2 seconds
Memory limit: 256 megabytes

Statement

Quack the Duck has a permutation^{\text{∗}} aa of length nn and an incomplete sequence b1,b2,,bnb_1,b_2,\ldots,b_n.

Each element of bb is either 1-1 or an integer from 11 to nn. Each integer from 11 to nn appears at most once in bb.

Quack hopes to complete bb into a permutation that commutes with aa. In other words, after replacing every 1-1 in bb, the equality abi=baia_{b_i}=b_{a_i} should hold for every 1in1 \le i \le n.

Ja the Ghost wants to help Quack. Among all possible ways to complete bb, he wants to find the lexicographically smallest^{\text{†}} one.

Determine whether such a completion exists. If it exists, output the lexicographically smallest valid permutation bb. Otherwise, report that it is impossible.

^{\text{∗}}A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

^{\text{†}}An array pp is lexicographically smaller than an array qq of the same size if and only if the following holds:

  • pqp \ne q, and in the first position where pp and qq differ, the array pp has a smaller element than the corresponding element in qq.

Input

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

The first line of each test case contains an integer nn (1n21051 \le n \le 2 \cdot 10^5) — the length of the permutation.

The second line of each test case contains nn integers a1,a2,,ana_1,a_2,\ldots,a_n (1ain1 \le a_i \le n) — the permutation aa.

The third line of each test case contains nn integers b1,b2,,bnb_1,b_2,\ldots,b_n (bi=1b_i=-1 or 1bin1 \le b_i \le n) — the incomplete sequence bb.

It is guaranteed that each integer from 11 to nn appears at most once in bb.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output

For each test case, print "YES" if the answer exists, and "NO" otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

If the answer exists, in the next line output nn integers p1,p2,,pnp_1,p_2,\ldots,p_n — the lexicographically smallest valid sequence after replacing every 1-1 in bb.

The sequence pp must be a permutation, that is, each integer from 11 to nn must appear exactly once in pp. Also, it must satisfy api=paia_{p_i}=p_{a_i} for every 1in1 \le i \le n.

Note

In the first test case, b=[1,2,3]b=[1,2,3] commutes with any permutation aa. Since all elements of bb are unknown, this is also the lexicographically smallest possible valid permutation.

In the second test case, a=[2,1,4,3]a=[2,1,4,3] and b3=4b_3=4. Since a3=4a_3=4, the condition for i=3i=3 gives ab3=ba3a_{b_3}=b_{a_3}, so a4=b4a_4=b_4, hence b4=3b_4=3. The remaining values are 11 and 22, and the lexicographically smallest valid choice is b1=1b_1=1, b2=2b_2=2. Thus the answer is [1,2,4,3][1,2,4,3].

In the third test case, a=[2,1,4,3]a=[2,1,4,3], b1=3b_1=3, and b2=1b_2=1. For i=1i=1, the condition requires ab1=ba1a_{b_1}=b_{a_1}. However, ab1=a3=4a_{b_1}=a_3=4, while ba1=b2=1b_{a_1}=b_2=1. Since 414 \neq 1, no valid completion exists.

Examples

12
3
2 3 1
-1 -1 -1
4
2 1 4 3
-1 -1 4 -1
4
2 1 4 3
3 1 -1 -1
4
2 1 4 3
1 -1 -1 2
5
2 3 1 5 4
2 -1 -1 -1 -1
5
2 3 1 5 4
4 -1 -1 -1 -1
6
2 3 1 5 6 4
4 -1 -1 -1 -1 -1
6
2 1 4 3 6 5
-1 3 -1 -1 -1 -1
6
3 5 6 2 1 4
-1 -1 -1 3 6 -1
7
2 3 1 5 4 6 7
-1 -1 -1 -1 -1 7 -1
8
2 3 4 1 6 7 8 5
5 7 -1 -1 -1 -1 -1 -1
8
2 3 4 1 6 7 8 5
5 -1 -1 -1 -1 -1 -1 -1
YES
1 2 3
YES
1 2 4 3
NO
NO
YES
2 3 1 4 5
NO
YES
4 5 6 1 2 3
YES
4 3 1 2 5 6
NO
YES
1 2 3 4 5 7 6
NO
YES
5 6 7 8 1 2 3 4