#P3346. [ZJOI2015] 诸神眷顾的幻想乡

    ID: 2395 远端评测题 1000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>字符串2015浙江后缀自动机,SAMO2优化

[ZJOI2015] 诸神眷顾的幻想乡

Description

Yuuka is the most popular cute girl in all of Gensokyo. Today is her 26002600-th birthday, and countless fans have come to the sunflower field in front of her house to celebrate.

The fans are very enthusiastic and spontaneously organized a series of performances for Yuuka. Of course, Yuuka is very happy.

At this moment, Yuuka notices something interesting: the sunflower field has nn empty plots.

In the past, for convenience, Yuuka built n1n-1 edges among these nn plots to connect them.

That is, these nn plots form a tree.

There are nn fans who have come to the sunflower field.

To express their congratulations for Yuuka’s birthday, they chose cc colors of clothing, and each color is represented by an integer between 00 and c1c-1.

Each person stands on exactly one plot, and each plot has exactly one person.

Thus, the entire sunflower field becomes colorful. Yuuka sees this and feels very happy.

One of the planned performances is as follows: select two fans A and B (A and B can be the same), then the fans on the path from A’s plot to B’s plot jump in order (including the endpoints).

In this way, Yuuka can see a color sequence whose length is the number of fans on the path between A and B (including A and B).

At first, everyone planned to try all pairs of fans. Note: A, B and B, A are different; the sequences they form are exactly reversed.

However, someone pointed out that this might lead to some identical color sequences, causing aesthetic fatigue.

So they want to ask: on this tree, how many different color sequences can Yuuka possibly see in total?

Due to the special structure of the sunflower field, the number of plots that are adjacent to exactly one plot does not exceed 2020.

Input Format

The first line contains two positive integers n,cn, c, denoting the number of plots and the number of colors.

The second line contains nn integers between 00 and c1c-1, separated by spaces, where the ii-th integer is the clothing color of the fan on the ii-th plot.

The next n1n-1 lines each contain two positive integers u,vu, v, indicating that there is an edge connecting plot uu and plot vv.

Output Format

Output a single line with one integer, denoting the answer.

7 3
0 2 1 2 1 0 0
1 2
3 4
3 5
4 6
5 7
2 5
30

Hint

Constraints

  • For 15%15\% of the testdata, n2×103n \le 2 \times 10^3.
  • For another 5%5\% of the testdata, every plot is adjacent to at most two plots.
  • For another 5%5\% of the testdata, except for one plot that is adjacent to three plots, every other plot is adjacent to at most two plots.
  • For another 5%5\% of the testdata, except for two plots that are adjacent to three plots, every other plot is adjacent to at most two plots.
  • For 100%100\% of the testdata, 1n1051 \le n \le 10^5, 1c101 \le c \le 10, 1u,vn1 \le u, v \le n.

Translated by ChatGPT 5