CF1675D.Vertical Paths

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a rooted tree consisting of nn vertices. Vertices are numbered from 11 to nn. 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 pp containing nn numbers: pip_i is a parent of the vertex with the index ii. The parent of a vertex uu is a vertex that is the next vertex on the shortest path from uu to the root. For example, on the simple path from 55 to 33 (the root), the next vertex would be 11, so the parent of 55 is 11.

The root has no parent, so for it, the value of pip_i is ii (the root is the only vertex for which pi=ip_i=i).

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);
  • number of paths is minimal.

For example, if n=5n=5 and p=[3,1,3,3,1]p=[3, 1, 3, 3, 1], then the tree can be divided into three paths:

  1. 3→1→53 \rightarrow 1 \rightarrow 5 (path of 33 vertices),
  2. 44 (path of 11 vertices).
  3. 22 (path of 11 vertices).

Example of splitting a root tree into three paths for n=5n=5, the root of the tree — node 33.

你将得到一棵包含 nn 个顶点的有根树。顶点编号为 11 到 nn。任意顶点均可作为树的根。

树是一种无环的连通无向图;有根树则是选定一个顶点作为根的树。

该树由一个长度为 nn 的父节点数组 pp 给出:pip_i 表示编号为 ii 的顶点的父节点。顶点 uu 的父节点,是指从 uu 到根的最短路径上紧邻 uu 的下一个顶点。例如,在从 55 到 33(根)的简单路径上,紧邻 55 的下一个顶点是 11,因此 55 的父节点为 11。

根节点没有父节点,故对根节点而言,其对应的 pi=ip_i = i(根节点是唯一满足 pi=ip_i = i 的顶点)。

请找出一组路径,使得:

  • 每个顶点恰好属于一条路径,每条路径可包含一个或多个顶点;
  • 在每条路径中,每个后续顶点均为当前顶点的一个子节点(即所有路径均严格向下延伸——从父节点指向子节点);
  • 路径总数最小。

例如,若 n=5n=5 且 p=[3,1,3,3,1]p=[3, 1, 3, 3, 1],则该树可被划分为三条路径:

  1. 3→1→53 \rightarrow 1 \rightarrow 5(含 33 个顶点的路径),
  2. 44(含 11 个顶点的路径),
  3. 22(含 11 个顶点的路径)。

将一棵以节点 33 为根的有根树划分为三条路径的示例(n=5n=5)。

输入格式

The first line of input data contains an integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases in the test.

Each test case consists of two lines.

The first of them contains an integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5). It is the number of vertices in the tree.

The second line contains nn integers p1,p2,…,pnp_1, p_2, \dots, p_n (1≤pi≤n1 \le p_i \le n). It is guaranteed that the pp array encodes some rooted tree.

It is guaranteed that the sum of the values nn over all test cases in the test does not exceed 2⋅1052 \cdot 10^5.

输入数据的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)——表示测试用例的数量。

每个测试用例由两行组成。

第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5),表示树中顶点的数量。

第二行包含 nn 个整数 p1,p2,…,pnp_1, p_2, \dots, p_n(1≤pi≤n1 \le p_i \le n)。保证数组 pp 编码了一棵有根树。

保证所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case on the first line, output an integer mm — the minimum number of non-intersecting leading down paths that can cover all vertices of the tree.

Then print mm 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.

If there are several answers, output any of them.

对于每个测试用例,在第一行输出一个整数 mm —— 覆盖树中所有顶点所需的互不相交的“自上而下”路径的最小数目。

然后输出 mm 对行,每对行描述一条路径:第一行输出该路径的长度,第二行输出该路径所经过的顶点序列(按从上到下的顺序)。你可以以任意顺序输出这些路径。

如果存在多个合法答案,输出其中任意一个即可。

输入输出样例

  • 输入#1

    6
    5
    3 1 3 3 1
    4
    1 1 4 1
    7
    1 1 2 3 4 5 6
    1
    1
    6
    4 4 4 4 1 2
    4
    2 2 2 2

    输出#1

    3
    3
    3 1 5
    1
    2
    1
    4
    
    2
    2
    1 2
    2
    4 3
    
    1
    7
    1 2 3 4 5 6 7
    
    1
    1
    1
    
    3
    3
    4 1 5
    2
    2 6
    1
    3
    
    3
    2
    2 1
    1
    3
    1
    4

输入解题思路,AI测评打分。不知道怎么写?

首页