AtCoder Regular Contest 209 (Div. 1)
D
A_A_i
AtCoder
⌀ 2583
原题
← CE →
翻译中…

Problem Statement

翻译中…

You are given a sequence $A = (A_1, A_2, \ldots, A_N)$ of length $N$. Each element is either an integer between $1$ and $N$ (inclusive) or $-1$.

翻译中…

Snuke has decided to create a new sequence using this sequence $A$.

翻译中…

First, replace all $-1$s in this sequence $A$ with integers between $1$ and $N$ (inclusive).

翻译中…

Next, create a sequence $B = (B_1, B_2, \ldots, B_N)$ of length $N$ according to the following formula:

翻译中…
  • $B_i=A_{A_i}$
翻译中…

Find the lexicographically smallest possible sequence $B$.

翻译中…

You are given $T$ test cases. Solve each of them.

翻译中…

What is the lexicographical order of sequences? A sequence $C = (C_1,C_2,\ldots,C_N)$ is lexicographically smaller than a sequence $D = (D_1,D_2,\ldots,D_N)$ if and only if there exists an integer $1 \leq i \leq N$ such that both of the following hold: $(C_1,C_2,\ldots,C_{i-1}) = (D_1,D_2,\ldots,D_{i-1})$ $C_i$ is (numerically) smaller than $D_i$.

翻译中…

Constraints

翻译中…
  • $1 \le T \le 10^5$
  • $1 \le N \le 5 \times 10^5$
  • $1 \le A_i \le N$ or $A_i = -1$.
  • The sum of $N$ over all test cases is at most $5\times 10^5$.
  • All input values are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

T
case_1
case_2
\vdots
case_T

翻译中…

Each case is given in the following format:

翻译中…

N
A_1 A_2 \dots A_N

翻译中…

Output

翻译中…

For each test case, output the lexicographically smallest possible sequence $B$, separated by newlines.

翻译中…

样例

Input #1
3
4
4 -1 -1 3
5
5 4 5 3 1
7
-1 6 5 -1 7 -1 6
Output #1
3 1 4 1
1 3 1 5 5
1 1 7 1 6 1 1
In the first test case, the sequence $A$ before Snuke's replacement is $(4,-1,-1,3)$. Suppose he replaces it to get $A=(4, 3, 1, 3)$. Then, $B=(A_4, A_3, A_1, A_3)=(3, 1, 4, 1)$. It is impossible to obtain a lexicographically smaller sequence $B$ than this.
题目信息
题号
arc209_d
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。