CF2223D.Zhily and Cycle
省选/NOI-
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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≤i≤n), there are directed edges from i to all vertices j such that ai≤j≤n.
Find a Hamiltonian cycle∗ of the graph.
∗A Hamiltonian cycle is a cycle that visits each vertex exactly once.
吉莉和吉莉决心环游世界,以消除一切混乱。他们从某个地点出发,希望恰好访问每个区域一次。
给你一个包含 n 个顶点的有向图,顶点编号为 1 到 n。对每个顶点 i(1≤i≤n),存在从 i 指向所有满足 ai≤j≤n 的顶点 j 的有向边。
请找出该图的一个哈密顿回路∗。
∗哈密顿回路是指恰好访问每个顶点一次的回路。
输入格式
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 a single integer n(1≤n≤105) — the number of vertices.
The second line of each test case contains n integers a1,a2,⋯,an(1≤ai≤n) describing the graph.
It is guaranteed that the sum of n across all test cases does not exceed 106.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤105) —— 表示顶点的数量。
每个测试用例的第二行包含 n 个整数 a1,a2,⋯,an(1≤ai≤n),用于描述该图。
保证所有测试用例中 n 的总和不超过 106。
输出格式
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 p1,p2,…,pn (1≤pi≤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.
对于每个测试用例,若不存在哈密顿回路,则在一行中输出“No”。
否则,在第一行输出“Yes”;在第二行输出一个排列 p1,p2,…,pn(其中 1≤pi≤n),表示该回路中顶点的访问顺序。若存在多个解,输出任意一个即可。
你可以以任意大小写形式输出答案(即大写或小写均可)。例如,字符串“yEs”、“yes”、“Yes”和“YES”均会被识别为肯定回答。
输入输出样例
输入#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
输出#1
Yes 7 5 2 4 3 6 1 No Yes 1 3 7 8 2 4 5 6 Yes 1 No
说明/提示
In the first test case, the figure below illustrates a Hamiltonian cycle: 1→7→5→2→4→3→6→1.

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).
在第一个测试用例中,下图展示了一个哈密顿回路:1→7→5→2→4→3→6→1。

在第二个测试用例中,不存在哈密顿回路,因为顶点 1 没有入边(即没有任何顶点能够到达顶点 1)。
输入解题思路,AI测评打分。不知道怎么写?