#P5984. [PA 2019] Podatki drogowe
[PA 2019] Podatki drogowe
Description
Given an unrooted tree with nodes, numbered from to . The weight of every edge is a positive integer power of .
Define the distance from to as the sum of edge weights along the unique simple path between and in the tree.
Given , among the values , find the -th smallest value.
Input Format
The first line contains two positive integers .
The next lines each contain three positive integers , indicating an edge connecting and with weight .
Output Format
Output one integer: the -th smallest value modulo .
5 8
1 2 1
3 1 3
3 4 1
5 3 2
135
Hint
For of the testdata, , and .
Sample Explanation
After sorting all , we get, in order: .
Translated by ChatGPT 5
京公网安备 11011102002149号