luogu#P7980. [JRKSJ R3] a question

[JRKSJ R3] a question

Background

This is a question.

Problem Description

Now there is a tree T\text{T} with nn nodes. The edges are weighted, and the node labels form a permutation of [1,n][1,n].

Let G\text{G} be the complement graph of T\text{T}. For each edge (x,y)(x,y) in G\text{G}, its weight is the sum of edge weights along the path xyx\rightarrow y in T\text{T}.

Let dis(x,y)dis(x,y) be the length of the shortest path between nodes xx and yy in G\text{G}. If xx and yy are not connected, define dis(x,y)=0dis(x,y)=0.

Compute $\displaystyle\sum_{i=1}^{n-1} \sum_{j=i+1}^n dis(i,j)$.

Input Format

All numbers in the input are integers.

The first line contains one integer nn.
The next n1n-1 lines each contain three integers u,v,wu,v,w, indicating that there is an edge in T\text{T} connecting uu and vv with weight ww.

Output Format

Output one integer representing the answer. Since the answer can be very large, output it modulo 109+710^9+7.

3
1 2 2
2 3 3
5
4
1 2 4
2 3 4
3 4 4
96

Hint

The complement graph G\text{G} of T\text{T} is defined as follows: for an edge (x,y)(1x,yn,xy)(x,y)(1\le x,y\le n,x\ne y), if this edge does not exist in T\text{T}, then it exists in G\text{G}; if this edge exists in T\text{T}, then it does not exist in G\text{G}. G\text{G} is an undirected graph.

Sample Explanation

For sample 22, the graph G\text{G} is shown in the figure:

3

We have: | dis(i,j)dis(i,j) | j=1j=1 | j=2j=2 | j=3j=3 | j=4j=4 | | :----------: | :----------: | :----------: | :----------: | :----------: | | i=1i=1 | | 2020 | 88 | 1212 | | i=2i=2 | | | 2828 | 88 | | i=3i=3 | | | | 2020 |

So the answer is 9696.

Constraints

Subtask\text{Subtask} nn\le Special Property Score Dependencies
11 10310^3 None 1010 None
22 10410^4 2020 11
33 2×1062\times 10^6 The tree is a star None
44 The tree is a chain
55 None 3030 1,2,3,41,2,3,4

For 100%100\% of the testdata, 2n2×1062\le n \le 2\times 10^6, 1x,yn1\le x,y\le n, 0v1090\le v\le 10^9.

The input file for this problem is large. Please use an appropriate fast input method.

Translated by ChatGPT 5