Number of Trees
Time limit2sMemory limit1024 MB
For a tree with vertex durabilities, sum the pieces left by each subtree after removing vertices with durability at most j, over all j from 1 to K.
- Level
Medium7 of 10
- Topics
- Tree, Dynamic programming, Sorting
- Solved
- No attempts yet
Problem
Jeonghu: Dong-hyeon, it is a Christmas tree!
- Dong-hyeon: Right! Suppose a subtree of the tree rots, and every vertex with durability is removed along with its incident edges. Then splits into pieces, right? (If everything is removed, .)
- Jeonghu: Right! For every subtree of , let be the sum of . What is the remainder when is divided by ?
- Dong-hyeon: Ah! I think we can solve it in less than .
How would that work? Let's try it ourselves.
Input
The first line contains and . The second line contains . From the third line through line , each line contains and , the two vertices joined by an edge of the tree. Every vertex can reach every other vertex through the edges.
Output
Print the remainder of divided by .
Constraints
- ,
- All given numbers are integers.