Lightning Energy Report
Time limit1sMemory limit256 MB
Given a tree and many path updates that each add a value to every vertex on a path, report the final total at every vertex.
- Level
Medium7 of 10
- Topics
- Tree, Prefix sum, DFS, Implementation
- Solved
- No attempts yet
Problem
The city of Thunder is a network of houses that run on lightning. Some pairs of houses are joined by wires, and the wires form a tree: between any two houses there is exactly one path. Every house has a battery that can hold an unlimited amount of energy, and at the start of the month every battery reads .
During the month, lightning strikes the city many times. Each strike is unusual: it hits two houses at the same instant — house with a red bolt and house with a blue bolt — and delivers units of energy to every house on the path from to (both endpoints included). That energy is added to each of those houses' batteries.
At the end of the month the mayor needs a report of the total energy stored in every house's battery. Produce that report from the month's observed strikes.
Input
The first line contains an integer () — the number of test cases. Each test case is given as follows.
- A line with an integer (), the number of houses, numbered through .
- lines, each with two integers and (), meaning houses and are joined by a wire. These wires always form a tree.
- A line with an integer (), the number of strikes.
- lines, each with three integers , , and (, ): a strike that adds units of energy to every house on the path from house to house . and may be equal, in which case only that single house is charged.
Output
For each test case, first print a line Case #X:, where is the test case number starting from . Then print lines: the -th line (for ) is the total energy stored in the battery of house at the end of the month.