問題一覧 > 通常問題

No.1103 Directed Length Sum

レベル : / AI推定難易度 : 900 / 実行時間制限 : 1ケース 3.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 212
作問者 : Ldio03 / テスター : jupiro
お気に入りにしたユーザー ProblemId : 4203 / 自分の提出
問題文最終更新日: 2020-07-03 23:46:41
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 255 (順位表) の他の問題:

Problem Statement

There is a directed tree with $N$ vertices, and the $i$-th edge goes from vertex $A_i$ to vertex $B_i$. Also, each vertex other than the root has one directed edge coming from its parent.
Define the function $f(a,b)$ as follows.
$f(a,b)=$ the length of the path from vertex $a$ to vertex $b$ (the number of edges traversed when going from vertex $a$ to vertex $b$)
However, if no path exists, let $f(a,b)=0$. Also, let $f(a,a)=0$.
Find $\displaystyle \sum_{i=1}^n \sum_{j=1}^n f(i,j)$. Since the answer can be large, find the remainder when it is divided by $10^9+7 $.

Notes

It has been confirmed that a solution passes with PyPy3, but this has not been confirmed with Python.

Input

$N$
$A_1\ B_1$
$A_2\ B_2$
:
$A_{N-1}\ B_{N-1}$

The constraints are as follows.
・$3 \le N \le 10^6$
・If $i \neq j $, then $(A_i,B_i) \neq (A_j,B_j)$
・$1 \le A_i,B_i \le N (1 \le i \le N - 1)$
・The given graph is a directed tree

Output

Output the answer. Print a newline at the end.

Sample

Sample 1
Input
3
1 2
1 3
Output
2

The value to find is $f(1,2)+f(1,3)+f(2,1)+f(2,3)+f(3,1)+f(3,2)$. Since paths of length $1$ exist from vertex $1$ to vertices $2,3$, we have $f(1,2)=f(1,3)=1$. On the other hand, for example, no path exists from vertex $2$ to vertex $3$, so $f(2,3)=0$.

Sample 2
Input
5
2 1
1 3
1 4
3 5
Output
13

For example, $f(3,4)=0,f(2,5)=3$. Note that the root is not necessarily vertex $1$. $($In this case, it is $2)$

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