1675D.md (2821B)
1 Source: https://codeforces.com/problemset/problem/1675/D 2 3 D. Vertical Paths 4 5 time limit per test 6 7 2 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 You are given a rooted tree consisting of $n$ vertices. Vertices are numbered from $1$ to $n$. Any vertex can be the root of a tree. 22 23 A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root. 24 25 The tree is specified by an array of parents $p$ containing $n$ numbers: $p_i$ is a parent of the vertex with the index $i$. The parent of a vertex $u$ is a vertex that is the next vertex on the shortest path from $u$ to the root. For example, on the simple path from $5$ to $3$ (the root), the next vertex would be $1$, so the parent of $5$ is $1$. 26 27 The root has no parent, so for it, the value of $p_i$ is $i$ (the root is the only vertex for which $p_i=i$). 28 29 Find such a set of paths that: 30 31 * each vertex belongs to exactly one path, each path can contain one or more vertices; 32 * in each path each next vertex — is a son of the current vertex (that is, paths always lead down — from parent to son); 33 * number of paths is minimal. 34 35 For example, if $n=5$ and $p=[3, 1, 3, 3, 1]$, then the tree can be divided into three paths: 36 37 1. $3 \rightarrow 1 \rightarrow 5$ (path of $3$ vertices), 38 2. $4$ (path of $1$ vertices). 39 3. $2$ (path of $1$ vertices). 40 41  Example of splitting a root tree into three paths for $n=5$, the root of the tree — node $3$. 42 43 Input 44 45 The first line of input data contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test. 46 47 Each test case consists of two lines. 48 49 The first of them contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$). It is the number of vertices in the tree. 50 51 The second line contains $n$ integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$). It is guaranteed that the $p$ array encodes some rooted tree. 52 53 It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2 \cdot 10^5$. 54 55 Output 56 57 For each test case on the first line, output an integer $m$ — the minimum number of non-intersecting leading down paths that can cover all vertices of the tree. 58 59 Then print $m$ pairs of lines containing path descriptions. In the first of them print the length of the path, in the second — the sequence of vertices specifying that path in the order from top to bottom. You can output the paths in any order. 60 61 If there are several answers, output any of them. 62 63 Example 64 65 Input 66 67 ``` 68 6 69 70 5 71 72 3 1 3 3 1 73 74 4 75 76 1 1 4 1 77 78 7 79 80 1 1 2 3 4 5 6 81 82 1 83 84 1 85 86 6 87 88 4 4 4 4 1 2 89 90 4 91 92 2 2 2 2 93 ``` 94 95 Output 96 97 ``` 98 3 99 3 100 3 1 5 101 1 102 2 103 1 104 4 105 106 2 107 2 108 1 2 109 2 110 4 3 111 112 1 113 7 114 1 2 3 4 5 6 7 115 116 1 117 1 118 1 119 120 3 121 3 122 4 1 5 123 2 124 2 6 125 1 126 3 127 128 3 129 2 130 2 1 131 1 132 3 133 1 134 4 135 ```