Codeforces Round 1097 (Div. 1, Based on Zhili Cup 2026)
D
Zhily and Cycle
Codeforces
2800
原题
← CE →
翻译中…

Zhily and Jily resolved to travel across the entire world to eliminate all chaos. They start from a certain place and wish to visit every region exactly once.

翻译中…

You are given a directed graph with $n$ vertices numbered from $1$ to $n$. For each vertex $i$ ($1 \le i \le n$), there are directed edges from $i$ to all vertices $j$ such that $a_i \le j \le n$.

翻译中…

Find a Hamiltonian cycle$^{\text{∗}}$ of the graph.

翻译中…

$^{\text{∗}}$A Hamiltonian cycle is a cycle that visits each vertex exactly once.

翻译中…

Input

翻译中…

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

翻译中…

The first line of each test case contains a single integer $n\,(1 \leq n \leq 10^5)$ — the number of vertices.

翻译中…

The second line of each test case contains $n$ integers $a_1,a_2,\cdots,a_n\,(1 \leq a_i \leq n)$ describing the graph.

翻译中…

It is guaranteed that the sum of $n$ across all test cases does not exceed $10^6$.

翻译中…

Output

翻译中…

For each test case, if no Hamiltonian cycle exists, output "No" in one line.

翻译中…

Otherwise, output "Yes" on the first line. On the second line, output a permutation $p_1, p_2, \dots, p_n$ ($1\le p_i\le n$) representing the order of vertices visited in the cycle. If there are multiple solutions, print any of them.

翻译中…

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.

翻译中…

Note

翻译中…

In the first test case, the figure below illustrates a Hamiltonian cycle: $1 \to 7 \to 5 \to 2 \to 4 \to 3 \to 6 \to 1$.

翻译中…

image

翻译中…

In the second test case, no Hamiltonian cycle exists because there are no incoming edges to vertex $1$ (i.e., no vertex can reach vertex $1$).

翻译中…

样例

Input #1
5
7
1 3 6 3 2 1 5
9
4 2 9 8 5 8 5 7 9
8
3 4 4 5 6 1 8 2
1
1
5
5 4 2 5 5
Output #1
Yes
7 5 2 4 3 6 1
No
Yes
1 3 7 8 2 4 5 6
Yes
1
No
InputCopy OutputCopy
题目信息
题号
2223D
时间限制
2 s
内存限制
512 MB
标签
图论
贪心

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