CF2237E.Permutation Commutation
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Quack the Duck has a permutation∗ a of length n and an incomplete sequence b1,b2,…,bn.
Each element of b is either −1 or an integer from 1 to n. Each integer from 1 to n appears at most once in b.
Quack hopes to complete b into a permutation that commutes with a. In other words, after replacing every −1 in b, the equality abi=bai should hold for every 1≤i≤n.
Ja the Ghost wants to help Quack. Among all possible ways to complete b, he wants to find the lexicographically smallest† one.
Determine whether such a completion exists. If it exists, output the lexicographically smallest valid permutation b. Otherwise, report that it is impossible.
∗A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array), and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
†An array p is lexicographically smaller than an array q of the same size if and only if the following holds:
- p=q, and in the first position where p and q differ, the array p has a smaller element than the corresponding element in q.
鸭子Quack有一个长度为 n 的排列∗ a 和一个不完整的序列 b1,b2,…,bn。
b 中的每个元素要么是 −1,要么是 1 到 n 之间的整数。1 到 n 中的每个整数在 b 中至多出现一次。
Quack 希望将 b 补充完整,使其成为一个与 a 可交换的排列。换言之,在将 b 中所有 −1 替换为适当值后,应对每个 1≤i≤n 满足等式 abi=bai。
幽灵Ja 想帮助 Quack。在所有可能的 b 的补全方案中,他希望找到字典序最小† 的那个。
请判断是否存在这样的补全方案。若存在,输出字典序最小的有效排列 b;否则,报告该问题无解。
∗ 长度为 n 的排列是指由 1 到 n 中 n 个互不相同的整数组成的任意顺序的数组。例如,[2,3,1,5,4] 是一个排列,但 [1,2,2] 不是排列(数组中 2 出现了两次),[1,3,4] 也不是排列(此时 n=3,但数组中出现了 4)。
† 当且仅当满足以下条件时,数组 p 字典序小于同长度的数组 q:
- p=q,且在 p 与 q 首次不同的位置上,p 在该位置的元素比 q 在对应位置的元素更小。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains an integer n (1≤n≤2⋅105) — the length of the permutation.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤n) — the permutation a.
The third line of each test case contains n integers b1,b2,…,bn (bi=−1 or 1≤bi≤n) — the incomplete sequence b.
It is guaranteed that each integer from 1 to n appears at most once in b.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)—— 排列的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤n)—— 排列 a。
每个测试用例的第三行包含 n 个整数 b1,b2,…,bn(bi=−1 或 1≤bi≤n)—— 不完整的序列 b。
保证 b 中每个 1 到 n 的整数至多出现一次。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
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 n integers p1,p2,…,pn — the lexicographically smallest valid sequence after replacing every −1 in b.
The sequence p must be a permutation, that is, each integer from 1 to n must appear exactly once in p. Also, it must satisfy api=pai for every 1≤i≤n.
对于每个测试用例,若答案存在,则输出 "YES";否则输出 "NO"。
你可以以任意大小写形式输出答案(大写或小写)。例如,字符串 "yEs"、"yes"、"Yes" 和 "YES" 均会被识别为肯定回答。
如果答案存在,则在下一行输出 n 个整数 p1,p2,…,pn —— 即在将 b 中所有 −1 替换后得到的字典序最小的有效序列。
序列 p 必须是一个排列,即 1 到 n 中的每个整数在 p 中必须恰好出现一次。此外,它还必须满足对每个 1≤i≤n,均有 api=pai。
输入输出样例
输入#1
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
输出#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
说明/提示
In the first test case, b=[1,2,3] commutes with any permutation a. Since all elements of b are unknown, this is also the lexicographically smallest possible valid permutation.
In the second test case, a=[2,1,4,3] and b3=4. Since a3=4, the condition for i=3 gives ab3=ba3, so a4=b4, hence b4=3. The remaining values are 1 and 2, and the lexicographically smallest valid choice is b1=1, b2=2. Thus the answer is [1,2,4,3].
In the third test case, a=[2,1,4,3], b1=3, and b2=1. For i=1, the condition requires ab1=ba1. However, ab1=a3=4, while ba1=b2=1. Since 4=1, no valid completion exists.
在第一个测试用例中,b=[1,2,3] 与任意排列 a 可交换。由于 b 的所有元素均未知,该排列也是字典序最小的合法排列。
在第二个测试用例中,a=[2,1,4,3] 且 b3=4。由于 a3=4,对 i=3 的条件给出 ab3=ba3,即 a4=b4,因此 b4=3。剩余待定值为 1 和 2,字典序最小的合法选择是 b1=1、b2=2。故答案为 [1,2,4,3]。
在第三个测试用例中,a=[2,1,4,3],b1=3 且 b2=1。对 i=1,条件要求 ab1=ba1。然而 ab1=a3=4,而 ba1=b2=1。由于 4=1,不存在合法的补全方案。
输入解题思路,AI测评打分。不知道怎么写?