Janjetina
Time limit1.5sMemory limit512 MB
Count ordered city pairs in a tree whose path's maximum lamb weight minus the path length is at least k.
- Level
Medium7 of 10
- Topics
- Tree, Divide and conquer, Sorting
- Solved
- No attempts yet
Problem
After restaurants all over Croatia closed because of the lockdown, Mr. Malnar was first overwhelmed with sadness. But he soon realized that there was no point in being sad, so he decided that as soon as the restaurants reopen, he will travel around Croatia and treat himself to the best lamb Croatian restaurants can offer.
Mr. Malnar knows about cities he could visit, labeled with integers from through . He also knows about two-way roads that connect these cities, so it is possible to travel between any two cities.
On every road there is a restaurant that serves lamb. Mr. Malnar knows how many kilograms of lamb he can order at each one.
He will choose two different cities and and travel from to via the shortest path, that is, the path that uses the fewest roads. He will stop at exactly one restaurant, the one where he can order the maximum amount of lamb (if several restaurants tie, he chooses any of them), and he will eat all the lamb he orders.
The length of a path is the number of roads it goes through. If he eats kilograms of lamb on a path of length , the path is satisfactory when .
He wonders how many ordered pairs of distinct cities have a satisfactory shortest path between them. Calculate the answer for him.
Input
The first line contains integers and (), the number of cities and the satisfaction threshold.
Each of the following lines contains three integers , and (, , ). This means there is a road connecting and , and a restaurant on that road where Mr. Malnar can order kilograms of lamb.
Output
Output the number of ordered pairs of distinct cities such that the shortest path from to is satisfactory.
Hint
Clarification of the third example:

The pairs are (1, 3), (3, 1), (1, 5), (5, 1), (3, 5), (5, 3), (4, 5) and (5, 4).