luogu#P4178. Tree

    ID: 3138 远端评测题 1000ms 500MiB 尝试: 1 已通过: 1 难度: 7 上传者: 标签>点分治树状数组分治容斥原理

Tree

Problem 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:

  • 1≤n≤4×1041 \le n \le 4 \times 10^4.
  • 1≤u,v≤n1 \le u, v \le n.
  • 0≤w≤1030 \le w \le 10^3.
  • 0≤k≤2×1040 \le k \le 2 \times 10^4.

Translated by ChatGPT 5