#P5298. [PKUWC2018] Minimax

    ID: 4243 远端评测题 1000ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2018线段树树形 dp概率论,统计

[PKUWC2018] Minimax

Description

Little C has a rooted tree with nn nodes. The root is node 11, and each node has at most two children.

Define the value of a node xx as follows:

  1. If xx has no children, then its value is given in the input. It is guaranteed that all such nodes have pairwise distinct values.

  2. If xx has children, then its value equals the maximum of its children’s values with probability pxp_x, and equals the minimum of its children’s values with probability 1px1-p_x.

Now Little C wants to know the following. Suppose the value of node 11 has mm possible outcomes. Let the value of the ii-th smallest outcome be ViV_i, and its probability be DiD_i (Di>0)(D_i>0). Compute:

i=1miViDi2\sum_{i=1}^{m}i\cdot V_i\cdot D_i^2

You need to output the answer modulo 998244353998244353.

Input Format

The first line contains a positive integer nn.

The second line contains nn integers. The ii-th integer is the index of the parent of node ii. The parent of node 11 is 00.

The third line contains nn integers. If node ii has no children, then the ii-th number is its value; otherwise, the ii-th number is pi10000p_i\cdot 10000. It is guaranteed that pi10000p_i\cdot 10000 is a positive integer.

Output Format

Output the answer.

3
0 1 1
5000 1 2
748683266

Hint

Sample Explanation

The value of node 11 is 11 with probability 12\frac{1}{2}, and 22 with probability 12\frac{1}{2}, so the answer is 54\frac{5}{4}.

Constraints

  • For 10%10\% of the testdata, 1n201\leq n\leq 20.
  • For 20%20\% of the testdata, 1n4001\leq n\leq 400.
  • For 40%40\% of the testdata, 1n50001\leq n\leq 5000.
  • For 60%60\% of the testdata, 1n1051\leq n\leq 10^5.
  • Another 10%10\% of the testdata guarantees that the shape of the tree is random.
  • For 100%100\% of the testdata, 1n3×1051\leq n\leq 3\times 10^5, 1wi1091\leq w_i\leq 10^9.

For all testdata, 0<pi10000<100000 < p_i \cdot 10000 < 10000, so it is easy to prove that every leaf value has a positive probability of being chosen by the root.

Translated by ChatGPT 5