CF2257C.Spying on the Beaver

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a rooted tree∗^{\text{∗}} with nn vertices, numbered from 11 to nn. The vertex numbered 11 is the root of the tree. The Beaver, initially located at the root, travels through the tree to one of the beaver dams located at the vertices numbered a1,…,ama_1, \ldots, a_m.

You need to determine which of these mm vertices the Beaver went to. To do this, you can place cameras on any edges of the tree. If the Beaver traverses an edge with a camera on it, you will see this. For clarity, we assume that after all the Beaver's movements, you will receive a sequence of edges with cameras in which the Beaver was observed passing through the corresponding edge.

Since cameras are expensive, it is necessary to use the minimum number of them sufficient to uniquely determine the Beaver's destination. You are required to state the minimum necessary number of cameras kk and the edges on which they should be placed.

∗^{\text{∗}}A rooted tree is a tree where one vertex is special and called the root.

你被给定一棵有 nn 个顶点的有根树∗^{\text{∗}},顶点编号为 11 到 nn。编号为 11 的顶点是该树的根。河狸最初位于根节点,并沿树中路径前往位于顶点 a1,…,ama_1, \ldots, a_m 处的某一座河狸水坝。

你需要确定河狸最终到达了这 mm 个顶点中的哪一个。为此,你可以在树的任意边上安装摄像头。若河狸经过一条装有摄像头的边,则你会观测到这一事件。为明确起见,我们假设:在河狸完成全部移动后,你将收到一个边序列(即所有装有摄像头且被河狸经过的边),按河狸经过它们的顺序排列。

由于摄像头价格昂贵,必须使用尽可能少的摄像头,且其数量须足以唯一确定河狸的目的地。你需要给出所需的最少摄像头数量 kk,以及应安装摄像头的具体边。

∗^{\text{∗}}有根树是指树中指定一个特殊顶点作为根的树。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤2⋅1041 \le t \le 2 \cdot 10^4). The description of the test cases follows.

In the first line of each test case, there is a single integer nn (2≤n≤1052 \le n \le 10^5).

The second line contains n−1n-1 integers p2,…,pnp_2, \ldots, p_n — the parents of the vertices from the 22nd to the nnth (1≤pi<i1 \le p_i \lt i; 2≤i≤n2 \le i \le n).

The third line contains a single integer mm — the number of vertices containing beaver dams (1≤m≤n1 \le m \le n).

In the fourth line, there are mm integers a1,…,ama_1, \ldots, a_m — the numbers of these vertices (1≤ai≤n1 \le a_i \le n; 1≤i≤m1 \le i \le m). All aia_i are distinct.

It is guaranteed that the sum of nn across all test cases does not exceed 10510^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤2⋅1041 \le t \le 2 \cdot 10^4)。随后是各测试用例的描述。

在每个测试用例的第一行中,有一个整数 nn(2≤n≤1052 \le n \le 10^5)。

第二行包含 n−1n-1 个整数 p2,…,pnp_2, \ldots, p_n —— 分别为第 22 个到第 nn 个顶点的父节点(1≤pi<i1 \le p_i \lt i;2≤i≤n2 \le i \le n)。

第三行包含一个整数 mm —— 含有海狸水坝的顶点数量(1≤m≤n1 \le m \le n)。

第四行包含 mm 个整数 a1,…,ama_1, \ldots, a_m —— 这些顶点的编号(1≤ai≤n1 \le a_i \le n;1≤i≤m1 \le i \le m)。所有 aia_i 互不相同。

保证所有测试用例的 nn 值之和不超过 10510^5。

输出格式

For each test case, output exactly one line. First, output the number kk — the minimum required number of cameras, and then, in the same line, for each of the kk edges connecting the vertices uu and pup_u, where cameras need to be installed, output the vertex number uu.

If there are multiple answers, you can output any one of them.

对于每个测试用例,输出恰好一行。首先输出数字 kk —— 所需摄像头的最少数量;然后在同一行中,对需要安装摄像头的 kk 条边(每条边连接顶点 uu 与 pup_u),依次输出顶点编号 uu。

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

输入输出样例

  • 输入#1

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

    输出#1

    0
    2 2 3
    1 3
    2 2 5

说明/提示

In the first test case, we do not need to install any cameras because there is only one dam in the tree, and the Beaver will definitely go there.

In the second test case, it is necessary to place a camera on each edge because there is a dam located at each vertex.

In the third test case, the dams are located in two adjacent vertices, so by placing a camera on the edge between these vertices, we can uniquely determine which one the Beaver went to.

在第一个测试用例中,我们无需安装任何摄像头,因为树中仅有一个水坝,海狸必定会前往该处。

在第二个测试用例中,必须在每条边上都安装一个摄像头,因为每个顶点上均设有一个水坝。

在第三个测试用例中,水坝位于两个相邻的顶点上,因此只需在连接这两个顶点的边上安装一个摄像头,即可唯一确定海狸前往的是哪一个顶点。

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

首页