#P9745. 「KDOI-06-S」树上异或

    ID: 10974 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP2023洛谷原创O2优化树形 DP位运算洛谷月赛

「KDOI-06-S」树上异或

Problem Description

Given a tree with nn nodes, the ii-th node has a node weight xix_i.

For the n1n-1 edges on the tree, each edge can be chosen to be deleted or kept, so there are 2n12^{n-1} ways to decide whether to delete each edge.

For each edge-deletion plan, suppose the resulting graph has kk 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 C1,C2,,CkC_1,C_2,\ldots,C_k, where CiC_i is the vertex set of the ii-th connected component, let vi=uCixuv_i=\bigoplus_{u\in C_i} x_u, then the value of this plan is v1×v2××vkv_1\times v_2\times \cdots\times v_k.

Find the sum of the values over all these 2n12^{n-1} edge-deletion plans, and output the answer modulo 998 244 353998~244~353.

Input Format

Read input from standard input.

The first line contains a positive integer nn, denoting the number of nodes in the tree.

The second line contains nn non-negative integers x1,x2,,xnx_1,x_2,\ldots,x_n, denoting the node weight of each node.

The third line contains n1n-1 positive integers f2,f3,,fnf_2,f_3,\ldots,f_n, indicating that there is an undirected edge between node ii and fif_{i}.

Output Format

Write output to standard output.

Output one line containing one integer, denoting the sum of the values over all 2n12^{n-1} edge-deletion plans, modulo 998 244 353998~244~353.

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 123=01\oplus2\oplus3=0.
  • Delete edge (1,2)(1,2): the graph has two connected components, and the value is (13)×2=4(1\oplus3)\times2=4.
  • Delete edge (1,3)(1,3): the graph has two connected components, and the value is (12)×3=9(1\oplus2)\times3=9.
  • Delete edges (1,2)(1,2) and (1,3)(1,3): the graph has three connected components, and the value is 1×2×3=61\times2\times3=6.

The total sum of values is 0+4+9+6=190+4+9+6=19.

[Sample #3]

See xor/xor3.in and xor/xor3.ans in the contestant directory.

This sample satisfies the constraints for test points 676\sim7.

[Sample #4]

See xor/xor4.in and xor/xor4.ans in the contestant directory.

This sample satisfies the constraints for test point 88.

[Sample #5]

See xor/xor5.in and xor/xor5.ans in the contestant directory.

This sample satisfies the constraints for test point 99.

[Sample #6]

See xor/xor6.in and xor/xor6.ans in the contestant directory.

This sample satisfies the constraints for test points 192119\sim21.


[Constraints]

For all data, it is guaranteed that: 1n5×1051\leq n\leq5\times10^5, 0xi10180\leq x_i\leq10^{18}, 1fi<i1\leq f_i<i.

Test Point ID nn\leq xix_i Special Property
121\sim2 1212 109\leq10^9 None
33 20002000 =1=1
44 10510^5 A
55 B
676\sim7 None
88 7\leq7 A
99 B
101110\sim11 None
121612\sim16 200200 8191\leq8191
1717 10510^5 109\leq10^9 A
1818 B
192119\sim21 None
222522\sim25 5×1055\times10^5 1018\leq10^{18}
  • Special Property A: it is guaranteed that for any 1<in1< i\le n, fi=i1f_i=i-1.
  • Special Property B: it is guaranteed that for any 1<in1< i\le n, fi=1f_i=1.

[Notes]

\oplus 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