結果
| 問題 | No.1103 Directed Length Sum | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-07-09 21:35:49 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,506 ms / 3,000 ms | 
| コード長 | 513 bytes | 
| コンパイル時間 | 2,232 ms | 
| コンパイル使用メモリ | 82,208 KB | 
| 実行使用メモリ | 282,828 KB | 
| 最終ジャッジ日時 | 2024-10-08 10:07:28 | 
| 合計ジャッジ時間 | 17,436 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 22 | 
ソースコード
n=int(input())
mod=10**9+7
es=[[] for _ in range(n)]
par=[-1]*n
for _ in range(n-1):
    a,b=map(int,input().split())
    es[a-1].append(b-1)
    if par[b-1]==-1:
        par[b-1]=a-1
for i in range(n):
    if par[i]==-1:
        root=i
        break
stack=[(root,0)]
ans=0
from collections import defaultdict
d=defaultdict(int)
while stack:
    now,depth=stack.pop()
    d[depth]+=1
    for e in es[now]:
        stack.append((e, depth+1))
for k in d.keys():
    ans+=d[k]*k*(k+1)//2
    ans%=mod
print(ans)
            
            
            
        