#P10175. 「OICon-02」Subtree Value
「OICon-02」Subtree Value
Problem Description
Given a tree with nodes, each node has a weight . 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 as . Compute the sum of the values of all connected subgraphs, modulo .
Input Format
The first line contains three positive integers , representing the number of nodes and the modulus ().
The second line contains positive integers , where is the parent of node when the tree is rooted at node .
The third line contains non-negative integers , 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 .
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 , the values of the following connected subgraphs are:
- : ;
- : ;
- : ;
- : ;
- : ;
- : .
The total sum is , and after taking modulo it is .
Constraints
This problem uses bundled testdata.
| Special Property | ||
|---|---|---|
| No special limits |
For of the data: , , , , .
Translated by ChatGPT 5