P14761 [CodeChef] FREQFUN
提高+
题目描述

Read problem statements in [Hindi],[Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.

Chef had a sequence $S_{0}, S_{1}, \ldots, S_{N-1}$; each element of this sequence was an integer between $0$ and $N-1$ (inclusive). Unfortunately, he forgot some (possibly zero or all) elements of this sequence. You are given a sequence $A_{0}, A_{1}, \ldots, A_{N-1}$, where for each valid $i$, $A_{i} = -1$ denotes an element Chef forgot and if $A_{i} \neq -1$, then $A_{i} = S_{i}$.

Before Chef forgot any elements of $S$, he created a sequence $B_{0}, B_{1}, \ldots, B_{N-1}$, where for each valid $i$, $B_{i}$ is the number of occurrences of the value $i$ in $S$ (the number of valid indices $j$ such that $S_{j} = i$), and then, he created a third sequence $G_{0}, G_{1}, \ldots, G_{N}$, where for each valid $i$, $G_{i}$ is the number of occurrences of the value $i$ in $B$. (Note that the elements of $B$ are between $0$ and $N$ inclusive.) Unfortunately, Chef also forgot the sequence $B$, but he remembers $G$.

Help Chef restore the missing elements of the sequence $S$. Precisely, find the lexicographically smallest sequence $S$ which is consistent with all the given information or determine that there is no such sequence.

------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{0}, A_{1}, \ldots, A_{N-1}$.
The third line contains $N+1$ space-separated integers $G_{0}, G_{1}, \ldots, G_{N}$.

------ Output ------
For each test case:
If there is no sequence $S$ consistent with all the given information, print a single line containing the string "impossible".
Otherwise, print a single line containing $N$ space-separated integers $S_{0}, S_{1}, \ldots, S_{N-1}$ - the lexicographically smallest valid sequence $S$.

------ Constraints ------
$1 ≤ T ≤ 1,000$
$2 ≤ N ≤ 10^{5}$
$-1 ≤ A_{i} ≤ N-1$ for each valid $i$
$0 ≤ G_{i} ≤ N$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$

------ Subtasks ------
Subtask #1 (50 points):
$1 ≤ N ≤ 50$
the sum of $N$ over all test cases does not exceed $500$

Subtask #2 (50 points): original constraints

----- Sample Input 1 ------
4

3

-1 0 -1

0 3 0 0

3

2 2 1

3 0 0 0

3

-1 2 -1

1 1 1 0

3

-1 2 2

0 1 1 0
----- Sample Output 1 ------
1 0 2

impossible

0 2 0

impossible

输入格式

见题面

输出格式

见题面

样例输入
4 3 -1 0 -1 0 3 0 0 3 2 2 1 3 0 0 0 3 -1 2 -1 1 1 1 0 3 -1 2 2 0 1 1 0
样例输出
1 0 2 impossible 0 2 0 impossible