結果
| 問題 | No.1103 Directed Length Sum | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-08-13 07:51:16 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,802 ms / 3,000 ms | 
| コード長 | 553 bytes | 
| コンパイル時間 | 192 ms | 
| コンパイル使用メモリ | 82,124 KB | 
| 実行使用メモリ | 236,840 KB | 
| 最終ジャッジ日時 | 2024-10-09 19:01:14 | 
| 合計ジャッジ時間 | 17,015 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 22 | 
ソースコード
from collections import deque
MOD = 10 ** 9 + 7
n = int(input())
root_cand = [True for _ in range(n)]
edges = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    edges[a - 1].append(b - 1)
    root_cand[b - 1] = False
root = root_cand.index(True)
ans = 0
queue = deque()
queue.append(root)
queue.append(0)
while queue:
    pos = queue.popleft()
    d = queue.popleft()
    ans += d * (d + 1) // 2
    ans %= MOD
    for n_pos in edges[pos]:
        queue.append(n_pos)
        queue.append(d + 1)
print(ans)
        
            
            
            
        