This page is still under construction.

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

Running Away From the Barn

Time limit1sMemory limit128 MB

Summary
For every node in a weighted tree rooted at node 1, count the descendants within total distance L along the downward path, including the node itself.
Level

Medium7 of 10

Topics
Tree, DFS, Binary search, Prefix sum
Solved
No attempts yet

Problem

It is milking time on the farm, but all the cows have run away! To round them up you first need to work out how far each cow could possibly have gone.

The farm has NN pastures (1≤N≤200,0001 \le N \le 200{,}000) numbered 1…N1 \dots N, connected by N−1N - 1 bidirectional paths. The barn is at pasture 11, and every pasture is reachable from the barn, so the pastures form a tree rooted at the barn.

Every cow starts the morning in its own pasture. A cow only runs away from the barn (it never moves back toward it), and it is too lazy to travel a total distance greater than LL. For every pasture, determine how many distinct pastures a cow starting there could end up in (including its starting pasture).

Because the distances can be large, store them in 64-bit integers.

Input

  • Line 11: two integers NN and LL (1≤N≤200,0001 \le N \le 200{,}000, 1≤L≤10181 \le L \le 10^{18}).
  • Lines 2…N2 \dots N: line ii contains two integers pip_i and lil_i. Here pip_i (1≤pi<i1 \le p_i < i) is the next pasture on the shortest path from pasture ii toward the barn (that is, the parent of pasture ii), and lil_i (1≤li≤10121 \le l_i \le 10^{12}) is the length of the path joining pasture ii and pasture pip_i.

Output

  • Lines 1…N1 \dots N: print one integer per line. The number on line ii is how many pastures can be reached from pasture ii by following paths that lead strictly farther away from the barn (pasture 11), such that the total distance travelled does not exceed LL.

Hint

In the example, cows from pasture 11 can hide in pastures 11, 22, and 44. Cows from pasture 22 can hide in pastures 22 and 33. Pastures 33 and 44 are the farthest from the barn, so a cow there can only stay put.

Examples2

  1. Example 1

    Input
    4 5
    1 4
    2 3
    1 5
    
    Expected output
    3
    2
    1
    1
    
  2. Example 2

    Input
    5 5
    1 2
    2 2
    3 2
    4 2
    
    Expected output
    3
    3
    3
    2
    1