#P9745. 「KDOI-06-S」树上异或
「KDOI-06-S」树上异或
Problem Description
Given a tree with nodes, the -th node has a node weight .
For the edges on the tree, each edge can be chosen to be deleted or kept, so there are ways to decide whether to delete each edge.
For each edge-deletion plan, suppose the resulting graph has connected components. Define the value of this plan as the product of the XOR sums of node weights within each connected component. Formally, if the graph contains connected components , where is the vertex set of the -th connected component, let , then the value of this plan is .
Find the sum of the values over all these edge-deletion plans, and output the answer modulo .
Input Format
Read input from standard input.
The first line contains a positive integer , denoting the number of nodes in the tree.
The second line contains non-negative integers , denoting the node weight of each node.
The third line contains positive integers , indicating that there is an undirected edge between node and .
Output Format
Write output to standard output.
Output one line containing one integer, denoting the sum of the values over all edge-deletion plans, modulo .
3
1 2 3
1 1
19
5
3 4 5 6 7
1 1 2 2
5985
Hint
[Sample Explanation #1]
There are four edge-deletion plans:
- Delete no edges: the graph has exactly one connected component, and the value is .
- Delete edge : the graph has two connected components, and the value is .
- Delete edge : the graph has two connected components, and the value is .
- Delete edges and : the graph has three connected components, and the value is .
The total sum of values is .
[Sample #3]
See xor/xor3.in and xor/xor3.ans in the contestant directory.
This sample satisfies the constraints for test points .
[Sample #4]
See xor/xor4.in and xor/xor4.ans in the contestant directory.
This sample satisfies the constraints for test point .
[Sample #5]
See xor/xor5.in and xor/xor5.ans in the contestant directory.
This sample satisfies the constraints for test point .
[Sample #6]
See xor/xor6.in and xor/xor6.ans in the contestant directory.
This sample satisfies the constraints for test points .
[Constraints]
For all data, it is guaranteed that: , , .
| Test Point ID | Special Property | ||
|---|---|---|---|
| None | |||
| A | |||
| B | |||
| None | |||
| A | |||
| B | |||
| None | |||
| A | |||
| B | |||
| None | |||
- Special Property A: it is guaranteed that for any , .
- Special Property B: it is guaranteed that for any , .
[Notes]
denotes the bitwise XOR operation.
The input and output size of this problem is large, so please use appropriate I/O methods.
Please pay attention to the impact of constant factors on the program’s running time.
Translated by ChatGPT 5