#CF2237E. 置换交换 / E. Permutation Commutation
置换交换 / 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 of length and an incomplete sequence .
Each element of is either or an integer from to . Each integer from to appears at most once in .
Quack hopes to complete into a permutation that commutes with . In other words, after replacing every in , the equality should hold for every .
Ja the Ghost wants to help Quack. Among all possible ways to complete , he wants to find the lexicographically smallest one.
Determine whether such a completion exists. If it exists, output the lexicographically smallest valid permutation . Otherwise, report that it is impossible.
A permutation of length is an array consisting of distinct integers from to in arbitrary order. For example, is a permutation, but is not a permutation ( appears twice in the array), and is also not a permutation ( but there is in the array).
An array is lexicographically smaller than an array of the same size if and only if the following holds:
- , and in the first position where and differ, the array has a smaller element than the corresponding element in .
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains an integer () — the length of the permutation.
The second line of each test case contains integers () — the permutation .
The third line of each test case contains integers ( or ) — the incomplete sequence .
It is guaranteed that each integer from to appears at most once in .
It is guaranteed that the sum of over all test cases does not exceed .
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 integers — the lexicographically smallest valid sequence after replacing every in .
The sequence must be a permutation, that is, each integer from to must appear exactly once in . Also, it must satisfy for every .
Note
In the first test case, commutes with any permutation . Since all elements of are unknown, this is also the lexicographically smallest possible valid permutation.
In the second test case, and . Since , the condition for gives , so , hence . The remaining values are and , and the lexicographically smallest valid choice is , . Thus the answer is .
In the third test case, , , and . For , the condition requires . However, , while . Since , 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