Tree of Charge
Time limit4sMemory limit512 MB
Charges on a rooted tree move up to parents, split down equally among children, or get added at a vertex. Print each final charge modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Tree, Math, Simulation
- Solved
- No attempts yet
Problem
A rooted tree is given. Each vertex holds a non-negative charge . You must process three kinds of queries.
- Move the charge up: all vertices simultaneously move their entire charge to their direct parent. The charge at the root is not moved anywhere. In other words, if vertex has children , then its new charge is when is not the root, and when is the root.
- Move the charge down: all vertices simultaneously split their charge equally among their children. If vertex has children , then the new charge of each is . Each leaf has a virtual straight line tree of sufficient length attached to it. So if you move the charge down from a leaf and then move it up the same number of times, the leaf keeps its original charge.
- Add charge: add to the charge of vertex .
After processing all queries, print the charge of every vertex.
Input
The first line contains the number of vertices (). The second line contains the initial charges (). Each of the next lines contains two integers and , an edge of the tree (). The input graph is guaranteed to be a tree.
The next line contains the number of queries (). Each of the next lines is one query. A move up query is written as ^. A move down query is written as v. An add charge query is written as + followed by two integers and , meaning add charge to vertex (, ).
Output
Print numbers. The -th number is the final charge of vertex .
Let the charge be the fraction . Print the unique integer with such that .