This page is still under construction.

Parts of this page are still being built. What you see may change.

Eagle Attack

Interview

Time limit7sMemory limit1024 MB

Summary
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 N−1N - 1 branches, which meet at NN different points that we call nodes (the trunk of the tree is node 11). Each branch of the tree therefore runs between two of these nodes. The old squirrel has predicted which of the tree's NN 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 vv into node uu, node uu starts shaking with strength vv. The shaking then spreads through the branches that leave node uu. 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 vv and spreads along kk branches, the shaking traveling along the branches has strength vk\frac{v}{k}. 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 NN (1≤N≤100 0001 \le N \le 100\,000), the number of nodes in the tree. The following N−1N-1 lines contain two integers aa and bb (1≤a,b≤N1 \le a,b \le N), which means there is a branch between node aa and node bb.

Then follows a line with the integer KK (1≤K≤100 0001 \le K \le 100\,000), the number of eagles that will attack. Finally, KK lines follow that describe the eagles in the order they crash into the tree. Each line contains two integers, the node uu (1≤u≤N1 \le u \le N) where the eagle will crash and the eagle's speed vv (1≤v≤1091 \le v \le 10^9).

Output

Print one line for each node in the order 11, 22, …\dots 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 10−510^{-5}.

Hint

Figure 1: The first eagle.

Figure 2: The second eagle.

The first eagle crashes into node 44 at speed 55. From node 44 the shaking spreads only to node 33, and from node 33 to both node 11 and node 55. From node 55 the shaking has nowhere to go, but from node 11 it spreads to node 22.

The second eagle crashes into node 33 at speed 66. From node 33 the shaking spreads to nodes 11, 44 and 55. The shakings in nodes 44 and 55 have nowhere to go, but the shaking in node 11 spreads to node 22.

To get the answers, add up the shakings of each node. For example, the answer at node 11 becomes 2.5+2=4.52.5+2=4.5 and at node 33 it becomes 5+6=115+6=11.

Examples2

  1. Example 1

    Input
    4
    1 2
    2 3
    3 4
    3
    2 7
    3 4
    2 3
    
    Expected output
    7
    12
    9
    7
    
  2. Example 2

    Input
    5
    1 2
    1 3
    3 4
    3 5
    2
    4 5
    3 6
    
    Expected output
    4.5
    4.5
    11
    7
    4.5