#P3346. [ZJOI2015] 诸神眷顾的幻想乡
[ZJOI2015] 诸神眷顾的幻想乡
Description
Yuuka is the most popular cute girl in all of Gensokyo. Today is her -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 empty plots.
In the past, for convenience, Yuuka built edges among these plots to connect them.
That is, these plots form a tree.
There are fans who have come to the sunflower field.
To express their congratulations for Yuuka’s birthday, they chose colors of clothing, and each color is represented by an integer between and .
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 .
Input Format
The first line contains two positive integers , denoting the number of plots and the number of colors.
The second line contains integers between and , separated by spaces, where the -th integer is the clothing color of the fan on the -th plot.
The next lines each contain two positive integers , indicating that there is an edge connecting plot and plot .
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 of the testdata, .
- For another of the testdata, every plot is adjacent to at most two plots.
- For another 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 of the testdata, except for two plots that are adjacent to three plots, every other plot is adjacent to at most two plots.
- For of the testdata, , , .
Translated by ChatGPT 5
京公网安备 11011102002149号