#P9593. 「Daily OI Round 1」Block
「Daily OI Round 1」Block
Problem Description
Given a tree whose nodes have colors, add edges between every pair of nodes whose distance on the tree is (while keeping the original edges). Find the number of non-empty connected vertex sets in the new graph such that all vertices in the set have the same color. Since the answer may be very large, output it modulo .
Definition of a connected vertex set: For a graph , a subset is a connected vertex set if and only if is a connected graph, where $E'=\{(u,v)\mid (u,v)\in E \land u \in V' \land v\in V'\}$.
Input Format
The first line contains a positive integer , the number of nodes.
The next line contains positive integers. The -th integer is the color of node .
The next lines each contain two positive integers , indicating that the original tree has an edge between and .
Output Format
Output one integer, the answer modulo .
4
1 2 1 1
1 2
2 3
2 4
8
6
1 2 2 2 1 2
5 3
2 1
4 5
6 3
3 1
14
16
1 1 2 1 1 2 2 2 1 1 2 1 1 1 2 1
12 8
14 9
10 8
1 16
7 12
6 1
14 8
3 1
12 5
1 13
12 2
1 12
15 8
11 5
4 12
442
16
11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11
4 14
4 15
12 13
2 5
7 15
10 2
15 8
15 13
9 11
13 11
3 15
8 16
6 13
1 4
10 4
27454
9
3 3 2 3 2 4 2 3 3
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
16
Hint
In Sample 1, the original tree is shown below:

After adding edges between nodes at distance on the tree, the new graph is shown below:

Then the non-empty connected vertex sets with the same color are: $\{1\},\{2\},\{3\},\{4\},\{1,3\},\{1,4\},\{3,4\},\{1,3,4\}$.
This problem uses bundled testdata.
| Score | Special Property | Dependency | ||
|---|---|---|---|---|
| A | None | |||
| None | ||||
| B | ||||
| C | ||||
| D | ||||
| None |
- Special Property A: All nodes have different colors.
- Special Property B: The given tree is a star (ju-hua tree, 菊花). Specifically, the -th edge connects node and node .
- Special Property C: The given tree is a chain. Specifically, the -th edge connects node and node .
- Special Property D: All nodes have the same color.
Constraints: For all testdata, , .
Translated by ChatGPT 5