問題一覧 > 通常問題

No.1333 Squared Sum

レベル : / AI推定難易度 : 1700 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 70
作問者 : penguinman / テスター : tpyneriver
お気に入りにしたユーザー ProblemId : 5436 / 自分の提出
問題文最終更新日: 2021-01-17 04:34:23
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 277 (順位表) の他の問題:

Problem Statement

You are given a tree with $N$ vertices. The $i$-th edge bidirectionally connects vertices $u_i$ and $v_i$, and its length is $w_i$.

Let $d(x,y)$ be the shortest distance between vertices $x$ and $y$.

Find $\displaystyle \sum_{i=1}^{N-1} \sum_{j=i+1}^{N}d(i,j)^2$.

Since this value can be very large, print the remainder when it is divided by $10^9+7$.

Input

$N$
$u_1\ v_1\ w_1$
$u_2\ v_2\ w_2$
$︙$
$u_{N-1}\ v_{N-1}\ w_{N-1}$
  • $1≤N≤2×10^5$
  • $1≤u_i,\ v_i≤N$
  • $u_i≠v_i$
  • $1≤w_i≤10^9$
  • The given graph is a tree.
  • All input values are integers.

Output

Print the remainder when $\displaystyle \sum_{i=1}^{N-1} \sum_{j=i+1}^{N}d(i,j)^2$ is divided by $10^9+7$.

Print a newline at the end.

Sample

Sample 1
Input
3
1 2 1
2 3 2
Output
14

We have $1^2+2^2+3^2=14$.

Sample 2
Input
5
1 3 4
2 5 3
3 4 9
5 4 1
Output
1046

Sample 3
Input
6
3 5 192329
4 6 293129
6 2 129532
1 2 39237
6 3 57325762
Output
219459352

Print the remainder when divided by $10^9+7$.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。