#P4178. Tree

Tree

Description

Given a tree with nn nodes where each edge has a weight, find the number of pairs of nodes whose distance on the tree is less than or equal to kk.

Input Format

The first line contains an integer nn, the number of nodes.

Lines 22 to nn each contain three integers u,v,wu, v, w, indicating there is an edge between uu and vv with weight ww.

Line n+1n+1 contains an integer kk.

Output Format

Output a single integer on one line, the answer.

7
1 6 13 
6 3 9 
3 5 7 
4 1 3 
2 4 20 
4 7 2 
10
5

Hint

Constraints

For all test points, it is guaranteed that:

  • 1n4×1041 \le n \le 4 \times 10^4.
  • 1u,vn1 \le u, v \le n.
  • 0w1030 \le w \le 10^3.
  • 0k2×1040 \le k \le 2 \times 10^4.

Translated by ChatGPT 5