#P6668. [清华集训 2016] 连通子树
[清华集训 2016] 连通子树
Description
Xiao D, a “Little M supremacist”, is in a very good mood today and plans to prepare a Christmas gift for Xiao M.
He bought a Christmas tree. This Christmas tree is a tree with nodes, and node has a color . Xiao D thinks it would not look good if some color appears too many times, so at most nodes have the same color.
Xiao M is very happy to receive the gift, but she has recently become interested in counting problems, so she has some questions for Xiao D to solve. Each time, Xiao M cares about three different colors . She wants to know how many different non-empty connected subgraphs of this Christmas tree satisfy that the numbers of nodes with colors inside are respectively . Since the number may be very large, you only need to output the remainder modulo .
Xiao D can of course solve this problem easily. Although you have been fed “dog food”, you also want to train your skills. Can you solve this problem?
Input Format
The first line contains two integers and , denoting the size of the tree and the number of queries.
The second line contains integers, where the -th integer is , the color of the -th node.
The next lines each contain two integers and , indicating that there is an edge between node and node .
The next lines each contain integers , describing a query as stated in the problem.
Output Format
For each query, output one line containing the answer.
5 3
1 2 3 1 2
1 2
2 3
3 4
4 5
1 1 2 1 3 1
1 0 2 1 3 1
1 1 2 1 3 0
3
1
2
Hint
Limits and Conventions
, .
For each query: , , and are pairwise distinct.
For of the queries, .
For another of the queries, , .
For another of the queries, .
For another of the queries, , and if the input tree is rooted at node , the maximum depth is at most (the depth of node is ).
For another of the queries, , and if the input tree is rooted at node , the maximum depth is at most (the depth of node is ).
For another of the queries, , and the sum over all queries of is at most .
For another of the queries, , and the sum over all queries of is at most .
Translated by ChatGPT 5
京公网安备 11011102002149号