結果
| 問題 | No.1103 Directed Length Sum | 
| コンテスト | |
| ユーザー |  Ldio03 | 
| 提出日時 | 2020-07-04 00:47:10 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,597 ms / 3,000 ms | 
| コード長 | 690 bytes | 
| コンパイル時間 | 158 ms | 
| コンパイル使用メモリ | 82,464 KB | 
| 実行使用メモリ | 223,416 KB | 
| 最終ジャッジ日時 | 2024-09-17 05:23:46 | 
| 合計ジャッジ時間 | 14,115 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 22 | 
ソースコード
from collections import deque
import sys
input=sys.stdin.readline
MOD = 10**9 + 7
inpl = lambda: list(map(int,input().split()))
N = int(input())
children = [[] for _ in range(N)]
parent = [None] * N
for _ in range(N-1):
    A, B = inpl()
    A, B = A-1, B-1
    children[A].append(B)
    parent[B] = A
root = parent.index(None)
pool = deque([root, -1])
ans = 0
d, n = 0, 0
while pool:
    node = pool.popleft()
    if node < 0:
        ans += (n * d * (d+1) // 2) % MOD
        ans %= MOD
        n = 0
        d += 1
        if pool:
            pool.append(-1)
        else:
            break
    else:
        n += 1
        for c in children[node]:
            pool.append(c)
print(ans)
            
            
            
        