Trees
Time limit2sMemory limit1024 MB
Given a rooted tree with per-vertex caps and subtree sum lower bounds, sum the minimum weighted cost over all cost vectors in given ranges, modulo 998244353.
- Level
Hard9 of 10
- Topics
- Tree, Dynamic programming, Combinatorics, Math
- Solved
- No attempts yet
Problem
A tree with vertices rooted at vertex 1 is given. Assign a nonnegative integer to each vertex .
The values must satisfy the following two conditions for every .
- Let be the sum of over all vertices in the subtree of vertex . Then .
For each that satisfies these conditions, let be the minimum value of .
Find the following value modulo .
Input
The first line contains , the number of vertices ().
Each of the next lines contains two integers and separated by a space, meaning there is an edge between and . ()
For each , the -th line contains , , , and separated by spaces. (; ; )
The given graph is a tree, and only inputs where a valid exists are given.
Output
Print the value from the statement modulo . Note that is prime.
Hint
Vertex is in the subtree of vertex if and only if or vertex is an ancestor of vertex .