Codeforces Round 695 (Div. 2)
E
Distinctive Roots in a Tree
Codeforces
2500
原题
← D→
翻译中…

You are given a tree with $n$ vertices. Each vertex $i$ has a value $a_i$ associated with it.

翻译中…

Let us root the tree at some vertex $v$. The vertex $v$ is called a distinctive root if the following holds: in all paths that start at $v$ and end at some other node, all the values encountered are distinct. Two different paths may have values in common but a single path must have all distinct values.

翻译中…

Find the number of distinctive roots in the tree.

翻译中…

Input

翻译中…

The first line of the input contains a single integer $n$ ($1 \le n \le 2\cdot10^5$) — the number of vertices in the tree.

翻译中…

The next line contains $n$ space-separated integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).

翻译中…

The following $n-1$ lines each contain two space-separated integers $u$ and $v$ ($1 \le u$, $v \le n$), denoting an edge from $u$ to $v$.

翻译中…

It is guaranteed that the edges form a tree.

翻译中…

Output

翻译中…

Print a single integer — the number of distinctive roots in the tree.

翻译中…

Note

翻译中…

In the first example, $1$, $2$ and $5$ are distinctive roots.

翻译中…

样例

Input #1
5
2 5 1 1 4
1 2
1 3
2 4
2 5
Output #1
3
InputCopy OutputCopy
Input #2
5
2 1 1 1 4
1 2
1 3
2 4
2 5
Output #2
0
InputCopy OutputCopy
题目信息
题号
1467E
时间限制
3 s
内存限制
256 MB
标签
数据结构
DFS 及类似
动态规划
树

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