#P10175. 「OICon-02」Subtree Value

    ID: 11373 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DPO2优化树形 DP组合数学

「OICon-02」Subtree Value

Problem Description

Given a tree with nn nodes, each node has a weight ava_v. Define a connected subgraph of a tree as a non-empty set of nodes in the tree such that these nodes form a connected block in the tree. Define the value of a connected subgraph SS as vS(av+S)\prod_{v\in S}(a_v+|S|). Compute the sum of the values of all connected subgraphs, modulo UVU^V.

Input Format

The first line contains three positive integers n,U,Vn, U, V, representing the number of nodes and the modulus (UVU^V).

The second line contains n1n-1 positive integers f2,f3,,fnf_2, f_3, \dots, f_n, where fif_i is the parent of node ii when the tree is rooted at node 11.

The third line contains nn non-negative integers aia_i, representing the weight of each node.

Output Format

One line containing one positive integer, representing the sum of the values of all connected subgraphs, modulo UVU^V.

3 10 6
1 1
1 2 3
156
11 4 6
1 1 2 3 4 4 4 5 6 7
325 190 400 325 380 165 334 400 80 171 340
678

Hint

Sample Explanation

For sample 11, the values of the following connected subgraphs are:

  • {1}\{1\}: (1+1)=2(1+1)=2;
  • {2}\{2\}: (2+1)=3(2+1)=3;
  • {3}\{3\}: (3+1)=4(3+1)=4;
  • {1,2}\{1,2\}: (1+2)×(2+2)=12(1+2)\times(2+2)=12;
  • {1,3}\{1,3\}: (1+2)×(3+2)=15(1+2)\times(3+2)=15;
  • {1,2,3}\{1,2,3\}: (1+3)×(2+3)×(3+3)=120(1+3)\times(2+3)\times(3+3)=120.

The total sum is 2+3+4+12+15+120=1562+3+4+12+15+120=156, and after taking modulo 10610^6 it is 156156.

Constraints

This problem uses bundled testdata.

Subtask\text{Subtask} Special Property Score\text{Score}
11 n10n\leq10 55
22 n150n\leq150 88
33 n500n\leq500 1111
44 U=2,V=1U=2,V=1 77
55 V=1V=1 2323
66 UaiU\mid a_i
77 No special limits

For 100%100\% of the data: 1n20001\leq n\leq2000, 1fi<i1\leq f_i<i, 2U102\leq U\leq10, 1V61\leq V\leq6, 0ai<UV0\leq a_i< U^V.

Translated by ChatGPT 5