CF1794E.Labeling the Tree with Distances
提高+/省选-
通过率:0%
时间限制:4.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an unweighted tree of n vertices numbered from 1 to n and a list of n−1 integers a1,a2,…,an−1. A tree is a connected undirected graph without cycles. You will use each element of the list to label one vertex. No vertex should be labeled twice. You can label the only remaining unlabeled vertex with any integer.
A vertex x is called good if it is possible to do this labeling so that for each vertex i, its label is the distance between x and i. The distance between two vertices s and t on a tree is the minimum number of edges on a path that starts at vertex s and ends at vertex t.
Find all good vertices.
给你一棵包含 n 个顶点(编号为 1 到 n)的无权树,以及一个由 n−1 个整数 a1,a2,…,an−1 构成的列表。树是一种连通的无向无环图。你需要用该列表中的每个元素恰好标记一个顶点,且每个顶点至多被标记一次。唯一尚未被标记的顶点可以被赋予任意整数。
若存在一种标记方式,使得对每个顶点 i,其标记值恰好等于顶点 x 与 i 之间的距离,则称顶点 x 是好顶点。树上两个顶点 s 和 t 之间的距离定义为从 s 到 t 的路径所经过的最少边数。
请找出所有好顶点。
输入格式
The first line contains one integer n (2≤n≤2⋅105) — the number of vertices in the tree.
The second line contains n−1 integers a1,a2,…,an−1 (0≤ai<n) — the given list.
Then, n−1 lines follow. Each of them contains two integers u and v (1≤u,v≤n) denoting an edge between vertices u and v. It is guaranteed that the edges form a tree.
第一行包含一个整数 n(2≤n≤2⋅105)——树中顶点的数量。
第二行包含 n−1 个整数 a1,a2,…,an−1(0≤ai<n)——给定的列表。
接下来是 n−1 行,每行包含两个整数 u 和 v(1≤u,v≤n),表示顶点 u 与 v 之间的一条边。保证这些边构成一棵树。
输出格式
In the first line print the number of good vertices.
In the second line, print the indices of all good vertices in ascending order.
第一行输出好顶点的数量。
第二行按升序输出所有好顶点的索引。
输入输出样例
输入#1
6 2 1 0 1 2 1 2 2 3 2 4 4 5 4 6
输出#1
2 2 4
输入#2
5 1 2 1 2 1 2 3 2 3 4 5 4
输出#2
1 3
输入#3
3 2 2 1 2 2 3
输出#3
0
说明/提示
This is the tree for the first example:

And these are two possible labelings with the elements on the list so that 2 is a good vertex (left) and 4 is a good vertex (right).

The square below each vertex represents its label. The black squares contain the numbers which were on the given list and the only white square contains the only number which was not on the given list.
In the second example, the only good vertex is vertex 3.
In the third example, there are no good vertices.
这是第一个示例对应的树:

以下是两种可能的顶点标号方式(使用给定列表中的元素),使得顶点 2 是一个好顶点(左图)和顶点 4 是一个好顶点(右图):

每个顶点下方的方块表示其标号。黑色方块中填入的是给定列表中出现的数字,唯一的一个白色方块中填入的是未出现在给定列表中的那个数字。
在第二个示例中,唯一的好顶点是顶点 3。
在第三个示例中,不存在好顶点。
输入解题思路,AI测评打分。不知道怎么写?