Codeforces Round 856 (Div. 2)
E
Labeling the Tree with Distances
Codeforces
2400
原题
← D→
翻译中…

You are given an unweighted tree of $n$ vertices numbered from $1$ to $n$ and a list of $n-1$ integers $a_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 $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.

翻译中…

Input

翻译中…

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

翻译中…

The second line contains $n - 1$ integers $a_1,a_2,\ldots,a_{n-1}$ ($0\le a_i \lt n$) — the given list.

翻译中…

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

翻译中…

Output

翻译中…

In the first line print the number of good vertices.

翻译中…

In the second line, print the indices of all good vertices in ascending order.

翻译中…

Note

翻译中…

This is the tree for the first example:

翻译中…

image

翻译中…

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).

翻译中…

image

翻译中…

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.

翻译中…

样例

Input #1
6
2 1 0 1 2
1 2
2 3
2 4
4 5
4 6
Output #1
2
2 4
InputCopy OutputCopy
Input #2
5
1 2 1 2
1 2
3 2
3 4
5 4
Output #2
1
3
InputCopy OutputCopy
Input #3
3
2 2
1 2
2 3
Output #3
0
InputCopy OutputCopy
题目信息
题号
1794E
时间限制
4 s
内存限制
256 MB
标签
数据结构
动态规划
贪心
哈希
模拟
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。