Vertical Paths Codeforces Solution | Codeforces Problem Solution 2022
You are given a rooted tree consisting of vertices. Vertices are numbered from to . Any vertex can be the root of a tree.
A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root.
The tree is specified by an array of parents containing numbers: is a parent of the vertex with the index . The parent of a vertex is a vertex that is the next vertex on the shortest path from to the root. For example, on the simple path from to (the root), the next vertex would be , so the parent of is .
The root has no parent, so for it, the value of is (the root is the only vertex for which ).
Find such a set of paths that:
- each vertex belongs to exactly one path, each path can contain one or more vertices;
- in each path each next vertex — is a son of the current vertex (that is, paths always lead down — from parent to son);
- amount of paths is minimal.
For example, if and , then the tree can be divided into three paths:
- (path of vertices),
- (path of vertices).
- (path of vertices).
The first line of input data contains an integer () — the number of test cases in the test.
Each test case consists of two lines.
The first of them contains an integer (). It is the number of vertices in the tree.
The second line contains integers (). It is guaranteed that the array encodes some rooted tree.
It is guaranteed that the sum of the values over all test cases in the test does not exceed .
For each test case on the first line, output an integer — the minimum amount of non-intersecting leading down paths that can cover all vertices of the tree.
Then print 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.
Join Now for Solution:-
No comments:
Post a Comment