codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

2171F.md (2915B)


      1 Source: https://codeforces.com/problemset/problem/2171/F
      2 
      3 F. Rae Taylor and Trees (hard version)
      4 
      5 time limit per test
      6 
      7 3 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 "To think a commoner would even fathom sitting next to me. Know your place!"
     22 
     23 — Claire François
     24 
     25 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.
     26 
     27 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!
     28 
     29 You are given a permutation$^{\text{∗}}$ $p$ of length $n$.
     30 
     31 Determine if there exists an undirected tree with $n$ vertices labeled $1, 2, \dots, n$, satisfying the following condition:
     32 
     33 * Let $u$ and $v$ ($1\leq {\color{red}{u \lt v}} \leq n$) be any two vertices connected by an edge. Then $u$ appears before $v$ in $p$.
     34 
     35 Additionally, if there exists such a tree, output any of them.
     36 
     37 $^{\text{∗}}$A permutation of length $n$ is an array that contains every integer from $1$ to $n$ exactly once, in any order.
     38 
     39 Input
     40 
     41 The first line contains a single integer $t$ ($1 \leq t \leq 10^4$)  — the number of test cases.
     42 
     43 The first line of each test case contains a single integer $n$ ($2\leq n\leq 2\cdot 10^5$).
     44 
     45 The second line of each test case contains $n$ integers, $p_1, p_2, \dots, p_n$ ($1\leq p_i\leq n$). It is guaranteed that all $p_i$ are distinct.
     46 
     47 It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$.
     48 
     49 Output
     50 
     51 For each test case, output on a single line "Yes" if there exists a tree satisfying the given condition, and "No" otherwise.
     52 
     53 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$.
     54 
     55 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".
     56 
     57 Example
     58 
     59 Input
     60 
     61 ```
     62 9
     63 
     64 6
     65 
     66 1 3 4 5 2 6
     67 
     68 4
     69 
     70 3 4 1 2
     71 
     72 5
     73 
     74 4 3 5 1 2
     75 
     76 4
     77 
     78 1 2 3 4
     79 
     80 7
     81 
     82 4 3 5 7 6 2 1
     83 
     84 6
     85 
     86 2 4 6 1 3 5
     87 
     88 3
     89 
     90 2 1 3
     91 
     92 4
     93 
     94 2 4 1 3
     95 
     96 6
     97 
     98 4 2 6 5 1 3
     99 ```
    100 
    101 Output
    102 
    103 ```
    104 Yes
    105 3 1
    106 4 1
    107 6 5
    108 6 2
    109 6 1
    110 No
    111 No
    112 Yes
    113 2 1
    114 4 3
    115 4 1
    116 No
    117 Yes
    118 4 2
    119 6 2
    120 3 1
    121 5 1
    122 5 2
    123 Yes
    124 3 2
    125 3 1
    126 Yes
    127 4 2
    128 3 1
    129 3 2
    130 Yes
    131 6 4
    132 6 2
    133 3 1
    134 5 4
    135 2 3
    136 ```
    137 
    138 Note
    139 
    140 In the first example, we can construct the tree given in the sample output. We have that
    141 
    142 * $1 \lt 3$, and $1$ appears before $3$ in $p$,
    143 * $1 \lt 4$, and $1$ appears before $4$ in $p$,
    144 * $5 \lt 6$, and $5$ appears before $6$ in $p$,
    145 * $2 \lt 6$, and $2$ appears before $6$ in $p$,
    146 * $1 \lt 6$, and $1$ appears before $6$ in $p$.
    147 
    148 In the second example, it can be shown that there does not exist a tree satisfying the given constraints.