#P3066. Count of Subtree Nodes within Distance t

    ID: 16324 Type: Default 1000ms 256MiB

Count of Subtree Nodes within Distance t

Count of Subtree Nodes within Distance t

Given a rooted tree with \(n\) nodes (numbered from 1 to \(n\) with node 1 as the root) and weighted edges, and a parameter \(t\), for every node \(u\) you are required to count the number of nodes in the subtree of \(u\) (including \(u\) itself) whose distance from \(u\) is at most \(t\). The distance between two nodes is defined as the sum of the weights along the unique path between them.

inputFormat

The first line contains two integers \(n\) and \(t\) (with constraints such as \(1 \le n \le 10^5\) and \(t \ge 0\)). Each of the next \(n-1\) lines contains three integers \(u\), \(v\), and \(w\) indicating that there is an edge from node \(u\) to node \(v\) with weight \(w\). It is guaranteed that the tree is rooted at node 1.

outputFormat

Output a single line with \(n\) space-separated integers. The \(i\)th integer should be the count of nodes in the subtree of node \(i\) (including node \(i\) itself) such that the distance from node \(i\) is at most \(t\).

sample

5 3
1 2 1
1 3 3
2 4 2
2 5 1
5 3 1 1 1