#P7283. [COCI 2020/2021 #4] Janjetina

[COCI 2020/2021 #4] Janjetina

Description

Mr. Malnar will visit nn cities, labeled with integers 11 to nn. He also found that there are n1n-1 bidirectional roads, each connecting two of the cities.

On each road there is a restaurant that serves lamb, and for each restaurant we are given the maximum weight of lamb that can be ordered.

He will choose two cities xx and yy, and travel from xx to yy along a shortest path (i.e., a path that uses the fewest roads). He will stop at exactly one restaurant, namely the one that can provide the largest amount of lamb (if there are multiple such restaurants, he may choose any one), and he will eat all the lamb he orders.

If along a path of length ll he can eat ww kilograms of lamb, and wlkw-l \ge k, then Mr. Malnar calls it satisfying. The length of a path is equal to the number of roads it uses.

Find how many ordered pairs (x,y)(x,y) make the shortest path from xx to yy satisfying.

Input Format

The first line contains integers n,kn,k, where nn is the number of cities.

The next n1n-1 lines each contain three integers x,y,wx,y,w, meaning there is a road connecting cities xx and yy, and on that road there is a restaurant that provides ww kilograms of lamb.

Output Format

Output the number of ordered pairs that satisfy the requirement.

3 1
1 2 3
1 3 2
6
4 1
1 2 1
2 3 2
3 4 3
6
5 2
1 2 2
1 3 3
3 4 2
3 5 4
8

Hint

Sample 1 Explanation

The ordered pairs that satisfy the requirement are (1,3),(3,1),(1,5),(5,1),(3,5),(5,3),(4,5),(5,4)(1,3),(3,1),(1,5),(5,1),(3,5),(5,3),(4,5),(5,4).

Constraints

This problem uses bundled evaluation.

Subtask Points Constraints and Notes
11 1515 1n10001 \le n \le 1000
22 3535 City ii and city i+1i+1 (1in11 \le i \le n-1) are directly connected (i.e., the shortest distance is 11).
33 6060 None.

For all testdata, 1n,k,w1051 \le n,k,w \le 10^5, 1x,yn1 \le x,y \le n, xyx \neq y.

Notes

The score setting follows the original COCI problem, with a full score of 110110.

Translated from COCI2020-2021 CONTEST #4 T4 Janjetina.

Translated by ChatGPT 5