CF1794E.Labeling the Tree with Distances

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

You are given an unweighted tree of nn vertices numbered from 11 to nn and a list of n1n-1 integers a1,a2,,an1a_1, a_2, \ldots, a_{n-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 xx is called good if it is possible to do this labeling so that for each vertex ii , its label is the distance between xx and ii . The distance between two vertices ss and tt on a tree is the minimum number of edges on a path that starts at vertex ss and ends at vertex tt .

Find all good vertices.

输入格式

The first line contains one integer nn ( 2n21052\le n\le 2\cdot 10^5 ) — the number of vertices in the tree.

The second line contains n1n - 1 integers a1,a2,,an1a_1,a_2,\ldots,a_{n-1} ( 0ai<n0\le a_i < n ) — the given list.

Then, n1n−1 lines follow. Each of them contains two integers uu and vv ( 1u,vn1\le u,v\le n ) denoting an edge between vertices uu and vv . It is guaranteed that the edges form a tree.

输出格式

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 22 is a good vertex (left) and 44 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 33 .

In the third example, there are no good vertices.

首页