#P7288. 「EZEC-5」树木的愤怒
「EZEC-5」树木的愤怒
Description
Little L and Little Y are good friends. One day, Little L brought a weighted tree with vertices. The weight of vertex is . However, Little Y hates trees, so without saying anything, he directly cut one edge of the tree.
Little L was very angry, but in order to stay polite, he decided to reconnect this edge after finishing his work. However, he always makes mistakes in his operations: not only can he not reconnect it, but he will also cut another edge. As a result, the tree is split into connected components.
Little Y could not stand it anymore. After cutting an edge, he started thinking about a difficult problem for him. He wants to know: after cutting the -th edge, since Little L will still mistakenly cut one more edge, what is the sum over all possible cases of the product of the sums of weights of the 3 connected components formed in the end. That is, let the three connected components be . Given that one edge has already been cut, compute the total sum of
$$(\sum_{u\in a} a_u)\times (\sum_{u\in b} a_u) \times (\sum_{u\in c} a_u)$$over all possibilities of the second cut.
Because of his anger, every time you finish the calculation, Little L ignores the answer you computed for Little Y. So Little Y has to restore the graph back to the original tree, cut another edge, and you must help Little Y compute it again, i.e., perform another (possibly different) query. You need to help Little Y for a total of times, meaning you must answer queries. Also, because both Little L and Little Y dislike very large numbers, please output this total sum modulo . Also, because printing is too time-consuming, you only need to output the sum and the xor-sum of the answers (each taken modulo ) over all queries.
Input Format
The first line contains two positive integers .
The second line contains non-negative integers representing .
In the following lines, the -th line contains two positive integers , representing the -th edge .
Then there are positive integers. The -th integer represents the edge cut in the -th query. Note that all queries are independent.
Output Format
Output two lines in total.
The first line outputs the sum of all answers modulo (note that the final output may be greater than or equal to ).
The second line outputs the xor-sum of all answers modulo .
4 3
1 2 3 4
1 2
2 3
3 4
1
2
3
140
40
7 2
1 1 1 1 1 1 1
1 2
1 3
2 6
2 4
1 5
3 7
2
6
70
52
Hint
Sample Explanation
For the first query of Sample 1, the first edge (i.e., edge ) has already been cut. If Little L cuts edge again, then the product of the sums of weights of the 3 connected components is . If Little L cuts edge again, then the product is . So the output is .
For the second query of Sample 1, the second edge (i.e., edge ) has already been cut. If Little L cuts edge again, then the product is . If Little L cuts edge again, then the product is . So the output is .
Similarly, we can compute the third query of Sample 1, whose answer is .
So the total sum of the three answers is , and the xor-sum is .
Constraints
。
。
。
。
。
No special constraints.
For all data, and .
idea by lgswdn
tested by LHQing, Karry5307
Translated by ChatGPT 5
京公网安备 11011102002149号