CF2171F.Rae Taylor and Trees (hard version)
普及/提高-
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
"To think a commoner would even fathom sitting next to me. Know your place!"
— Claire François
This is the hard version of the problem. The only difference between the easy and hard versions is that the hard version asks you to construct an example of a satisfactory tree.
As an Earth mage, Rae has mastered the spell of growing trees! But Manaria brags that she can grow a more impressive species of trees. Rae remembers that the most rare type of tree can be grown using a formula represented by a certain permutation — please help her construct it!
You are given a permutation∗ p of length n.
Determine if there exists an undirected tree with n vertices labeled 1,2,…,n, satisfying the following condition:
- Let u and v (1≤u<v≤n) be any two vertices connected by an edge. Then u appears before v in p.
Additionally, if there exists such a tree, output any of them.
∗A permutation of length n is an array that contains every integer from 1 to n exactly once, in any order.
“竟敢妄想坐在我身边,真是不知天高地厚!”
——克莱尔·弗朗索瓦
本题为该问题的困难版本。简单版本与困难版本的唯一区别在于:困难版本要求你构造一棵满足条件的树。
作为一名大地法师,蕾已精通培育树木的法术!但玛纳里亚却夸口说她能培育出更令人惊叹的树种。蕾想起,最稀有的树种可通过某个排列所表示的公式来培育——请帮她构造出这样的排列!
给定一个长度为 n 的排列∗ p。
请判断是否存在一棵含 n 个顶点(顶点编号为 1,2,…,n)的无向树,满足如下条件:
- 设 u 和 v(其中 1≤u<v≤n)是任意一条边所连接的两个顶点,则 u 在排列 p 中必须出现在 v 之前。
若存在这样的树,请输出其中任意一棵。
∗ 长度为 n 的排列是指由 1 到 n 的每个整数恰好出现一次所构成的数组(顺序任意)。
输入格式
The first line contains a single integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains a single integer n (2≤n≤2⋅105).
The second line of each test case contains n integers, p1,p2,…,pn (1≤pi≤n). It is guaranteed that all pi are distinct.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
第一行包含一个整数 t(1≤t≤104)——测试用例的数量。
每个测试用例的第一行包含一个整数 n(2≤n≤2⋅105)。
每个测试用例的第二行包含 n 个整数 p1,p2,…,pn(1≤pi≤n)。保证所有 pi 互不相同。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, output on a single line "Yes" if there exists a tree satisfying the given condition, and "No" otherwise.
Then, if the answer is "Yes", output n−1 lines. The i-th of these lines should contain two integers u and v, denoting an edge connecting vertices u and v.
You may output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "YES", and "yeS" will be recognized as "Yes".
对于每个测试用例,如果存在一棵满足给定条件的树,则在一行中输出“Yes”,否则输出“No”。
若答案为“Yes”,则接下来输出 n−1 行;其中第 i 行应包含两个整数 u 和 v,表示一条连接顶点 u 和 v 的边。
你可以以任意大小写形式输出答案(例如字符串 “yEs”、“yes”、“YES” 和 “yeS” 均会被识别为 “Yes”)。
输入输出样例
输入#1
9 6 1 3 4 5 2 6 4 3 4 1 2 5 4 3 5 1 2 4 1 2 3 4 7 4 3 5 7 6 2 1 6 2 4 6 1 3 5 3 2 1 3 4 2 4 1 3 6 4 2 6 5 1 3
输出#1
Yes 3 1 4 1 6 5 6 2 6 1 No No Yes 2 1 4 3 4 1 No Yes 4 2 6 2 3 1 5 1 5 2 Yes 3 2 3 1 Yes 4 2 3 1 3 2 Yes 6 4 6 2 3 1 5 4 2 3
说明/提示
In the first example, we can construct the tree given in the sample output. We have that
- 1<3, and 1 appears before 3 in p,
- 1<4, and 1 appears before 4 in p,
- 5<6, and 5 appears before 6 in p,
- 2<6, and 2 appears before 6 in p,
- 1<6, and 1 appears before 6 in p.
In the second example, it can be shown that there does not exist a tree satisfying the given constraints.
在第一个例子中,我们可以构造出样例输出中给出的树。我们有:
- 1<3,且 1 在排列 p 中出现在 3 之前,
- 1<4,且 1 在排列 p 中出现在 4 之前,
- 5<6,且 5 在排列 p 中出现在 6 之前,
- 2<6,且 2 在排列 p 中出现在 6 之前,
- 1<6,且 1 在排列 p 中出现在 6 之前。
在第二个例子中,可以证明不存在满足给定约束条件的树。
输入解题思路,AI测评打分。不知道怎么写?