#P6534. [COCI 2015/2016 #1] UZASTOPNI

[COCI 2015/2016 #1] UZASTOPNI

Description

Here is a tree with nn nodes. Each node on the tree has a weight viv_i. The tree is rooted at 11, and the nodes are numbered from 11 to nn.

Now we want to select some nodes and satisfy the following conditions:

  1. If a node’s parent is not selected, then this node cannot be selected.
  2. Within the set of selected nodes, there cannot be duplicate weights.
  3. For every selected node, the weights of all selected nodes in its subtree must be able to form an arithmetic progression with common difference 11.

You only need to output the number of selection plans that satisfy the conditions above.

Note: a “plan” here means different sets of selected weights.

Input Format

The first line contains an integer nn.

The next line contains nn integers viv_i.

The next n1n-1 lines each contain two integers ai,bia_i, b_i. The ii-th line describes an edge in the tree where aia_i is the parent and bib_i is the child.

Output Format

Only one line with one integer, representing the number of plans that satisfy the conditions above.

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

Hint

Sample Explanation

Sample 1 Explanation

The following are the six possible sets of selected weights:

$\{2\},\{2,3\},\{2,3,4\},\{1,2,3,4\},\{1,2\},\{1,2,3\}$.

Sample 2 Explanation

The following are the three possible sets of selected weights:

{3},{3,4},{3,4,5}\{3\},\{3,4\},\{3,4,5\}.

Note that you cannot select the node with weight 66, because the subtree {4,6}\{4,6\} formed by it and its parent does not meet the condition.

Constraints and Limits

  • For 50%50\% of the testdata, it is guaranteed that n100n \le 100.
  • For 100%100\% of the testdata, it is guaranteed that 1n1041 \le n \le 10^4, 1vi1001 \le v_i \le 100, 1ai,bin1 \le a_i, b_i \le n.

Notes

This problem is worth 160160 points in total.

This problem is translated from Croatian Open Competition in Informatics 2015/2016 Contest #1 T6 UZASTOPNI。

Translated by ChatGPT 5