#P8934. [JRKSJ R7] TSM eerT
[JRKSJ R7] TSM eerT
Problem Description
For a weighted tree with nodes, define as the sum of edge weights on the path in . Then define an undirected complete graph with nodes, where for all , the weight of edge in is .
Define as the maximum spanning tree of . In particular, if the maximum spanning tree of is not unique, you must determine it immediately and report it.
Given a tree and an integer , find . Its definition is given below.
Input Format
The first line contains two integers .
In the next lines , line contains two integers , representing an edge of with weight . That is, this line inputs two integers , and the real .
Output Format
Output only one integer as the answer.
If such that the maximum spanning tree of is not unique, output . Otherwise, output the sum of all edge weights of modulo .
3 3
1 1
2 2
13
10 2
1 7
1 2
1 5
4 5
2 1
3 9
2 9
4 4
9 4
736
4 1
1 1
2 1
3 1
-1
Hint
Definition
The definition of is:
$$f^k(T)=\begin{cases}T&k=0\\f(f^{k-1}(T))&k>0\end{cases}$$Explanation for Sample

They are .
Take the process of computing as an example. The generated is

The edges in the maximum spanning tree are .
Constraints
This problem uses bundled testdata.
For of the data, , , , .
Special Scoring Method
This problem enables subtask dependency, as follows:
- For subtask , you need to solve all subtasks to get the score of subtask .
Translated by ChatGPT 5