結果
| 問題 | No.1103 Directed Length Sum | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-07-09 20:56:40 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,690 ms / 3,000 ms | 
| コード長 | 526 bytes | 
| コンパイル時間 | 637 ms | 
| コンパイル使用メモリ | 82,000 KB | 
| 実行使用メモリ | 276,528 KB | 
| 最終ジャッジ日時 | 2024-10-08 06:55:52 | 
| 合計ジャッジ時間 | 17,958 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| 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
new=[]
# dfs
from collections import deque, Counter
stack=deque([(root,0)])
ans=0
while stack:
    now,depth=stack.pop()
    new.append((now,depth))
    for e in es[now]:
        stack.append((e, depth+1))
        ans+=(depth+1)*(depth+2)//2
        ans%=mod
print(ans%mod)
            
            
            
        