#P7815. 「小窝 R3」自傷無色
「小窝 R3」自傷無色
Description
You are given a tree with nodes, rooted at , with edge weights. Define the path length between two nodes on the tree as the sum of edge weights along the path from to .
For an unordered pair , define a "tree triangle" if and only if all of the following hold:
- Let be the lowest common ancestor of . Then and .
- Using , , and some positive integer as side lengths, a triangle can be formed. The value can be chosen arbitrarily, so a pair may produce multiple tree triangles.
In this case, is called the size of this tree triangle. See the sample explanation for a concrete example.
Two tree triangles are considered different if at least one of the following conditions holds:
- The unordered pair is different.
- The size of the tree triangle is different.
For a weighted tree , define its sine value as the ratio of:
- the sum of sizes of all tree triangles in ,
to
- the total number of distinct tree triangles in .
Little H gives you and wants you to compute . To avoid precision issues, output the result modulo . In particular, if there are no tree triangles in , then .
Input Format
The first line contains a positive integer , the number of nodes in the tree.
The next lines each contain three positive integers , indicating that there is an edge of weight between nodes and .
Output Format
Output one line: the value of modulo . The testdata guarantees that the total number of distinct tree triangles in is not a multiple of .
5
1 2 2
1 3 3
2 4 1
2 5 2
214285725
9
1 2 9
1 3 3
2 4 5
2 5 7
2 6 2
1 7 1
3 8 6
3 9 4
662721928
Hint
Sample Explanation
For sample 1, is shown in the figure below.

The triangle cycles formed by nodes are: $\underline{2,3},2;~\underline{2,3},3;~\underline{2,3},4$.
The triangle cycles formed by nodes are: $\underline{3,3},1;~\underline{3,3},2;~\underline{3,3},3;~\underline{3,3},4;~\underline{3,3},5$.
The triangle cycles formed by nodes are: $\underline{3,4},2;~\underline{3,4},3;~\underline{3,4},4;~\underline{3,4},5;~\underline{3,4},6$.
The triangle cycle formed by nodes is: .
Sum of all triangle cycle sizes: .
Total number of triangle cycles: .
, and the result modulo is .
Constraints
This problem uses bundled testdata.
- Special Property A: There exists a node in with degree .
- Special Property B: Except for leaf nodes, every node has degree .
- Special Property C: is a perfect binary tree.
| Subtask | Score | Special Property | |
|---|---|---|---|
| None | |||
| A | |||
| B | |||
| C | |||
| None |
For all testdata, and .
Notes
In the attached file depression_sample.zip:
depression_sample1.inis sample #1.depression_sample2.insatisfies Special Property A.depression_sample3.insatisfies Special Property B.depression_sample4.insatisfies Special Property C.depression_sample5.indoes not satisfy any special property.
Translated by ChatGPT 5
京公网安备 11011102002149号