Tri-State-Area<May Cir 19>

4.5

2 votes
Data Structures, Disjoint Data Structures, Disjoint Set, Medium
Problem

Given a weighted tree (T) and an integer MAXW, count the number of weighted graphs whose non-negative edges weight at most MAXW and T is an MST (minimum spanning tree) for that graph. Output the result modulo 987654319.

Input

The first line of input contains two integers n and MAXW, the number of vertices of the aforementioned tree and the maximum allowed weight of an edge respectively (1n300000).

The next n1 lines describes the tree. Each of which contain three integers, vi, ui and wi, meaning that there's and edge connecting vertices vi and ui with weight equal to wi (0MAXW,wi109).

It's guaranteed that the given graph forms a tree.

Output

Print only one integer, the number of desired graphs modulo 987654319.

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

It can easily be shown that the only other graph edge which isn't present in the tree (23) should weight at least 3. So there are 4 graphs in total matching the condition (one of them doesn't contain this edge).

Editor Image

?