Eagle Attack
InterviewTime limit7sMemory limit1024 MB
For each node of a tree, sum the strengths of shakes that spread outward from K crash points, halving (splitting by degree) at each branch.
- Level
Medium7 of 10
- Topics
- Tree, DFS, Math, Implementation
- Solved
- No attempts yet
Problem
Squirrels and eagles have been at war since time immemorial. The old squirrel is a master of astrology and has predicted that the eagles will soon attempt one final attack on the great tree. According to the old squirrel, several eagles will fly into the tree at high speed to make the whole tree shake, so that the squirrels risk falling down.
The tree consists of branches, which meet at different points that we call nodes (the trunk of the tree is node ). Each branch of the tree therefore runs between two of these nodes. The old squirrel has predicted which of the tree's nodes each of the eagles will crash into and at what speed. He wants to know how much each node will shake during the attack so that he can warn all the squirrels about the most dangerous nodes. Unfortunately, the old squirrel is not as good at programming as he is at astrology, so he has hired you to work out how much each node will shake during the attack.
When an eagle crashes at speed into node , node starts shaking with strength . The shaking then spreads through the branches that leave node . When the shaking reaches a node, it spreads through all branches that meet at the new node, except the one the shaking came from. The shaking strength is divided equally along these new branches, so if the shaking had strength and spreads along branches, the shaking traveling along the branches has strength . This continues until the shaking finally reaches nodes that have no branches other than the one the shaking came from, where the shaking stops spreading.
You may assume that the shaking from one crash has time to spread through the whole tree and die out before the next eagle crashes. For each of the nodes in the tree, the old squirrel wants to know the sum of the strengths of all shakings that the node will be subjected to.
Input
The first line contains the integer (), the number of nodes in the tree. The following lines contain two integers and (), which means there is a branch between node and node .
Then follows a line with the integer (), the number of eagles that will attack. Finally, lines follow that describe the eagles in the order they crash into the tree. Each line contains two integers, the node () where the eagle will crash and the eagle's speed ().
Output
Print one line for each node in the order , , with the sum of the strengths of all shakings the node will be subjected to. Your answer is considered correct if it has an absolute or relative error of at most .
Hint

Figure 1: The first eagle.

Figure 2: The second eagle.
The first eagle crashes into node at speed . From node the shaking spreads only to node , and from node to both node and node . From node the shaking has nowhere to go, but from node it spreads to node .
The second eagle crashes into node at speed . From node the shaking spreads to nodes , and . The shakings in nodes and have nowhere to go, but the shaking in node spreads to node .
To get the answers, add up the shakings of each node. For example, the answer at node becomes and at node it becomes .