#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 nn nodes, and node ii has a color cic_i. Xiao D thinks it would not look good if some color appears too many times, so at most 55 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 a,b,ca,b,c. She wants to know how many different non-empty connected subgraphs of this Christmas tree satisfy that the numbers of nodes with colors a,b,ca,b,c inside are respectively na,nb,ncna,nb,nc. Since the number may be very large, you only need to output the remainder modulo 109+710^9+7.

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 nn and QQ, denoting the size of the tree and the number of queries.

The second line contains nn integers, where the ii-th integer is cic_i, the color of the ii-th node.

The next n1n-1 lines each contain two integers aa and bb, indicating that there is an edge between node aa and node bb.

The next QQ lines each contain 66 integers a,na,b,nb,c,nca,na,b,nb,c,nc, 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

1in1≤i≤n, 1cin1≤c_i≤n.

For each query: 0na,nb,nc50≤na,nb,nc≤5, 1a,b,cn1≤a,b,c≤n, and a,b,ca,b,c are pairwise distinct.

For 10%10\% of the queries, n,Q10n,Q≤10.

For another 10%10\% of the queries, n15n≤15, Q50Q≤50.

For another 10%10\% of the queries, n,Q1000n,Q≤1000.

For another 5%5\% of the queries, n,Q50000n,Q≤50000, and if the input tree is rooted at node 11, the maximum depth is at most 3535 (the depth of node 11 is 00).

For another 5%5\% of the queries, n,Q100000n,Q≤100000, and if the input tree is rooted at node 11, the maximum depth is at most 3535 (the depth of node 11 is 00).

For another 20%20\% of the queries, n,Q100000n,Q≤100000, and the sum over all queries of (na+1)2(nb+1)2(nc+1)2(na+1)^2(nb+1)^2(nc+1)^2 is at most 10810^8.

For another 40%40\% of the queries, n,Q100000n,Q≤100000, and the sum over all queries of (na+1)(nb+1)(nc+1)(na+1)(nb+1)(nc+1) is at most 8×1068\times 10^6.

Translated by ChatGPT 5