結果

問題 No.1103 Directed Length Sum
ユーザー Ldio03
提出日時 2020-07-04 00:41:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,558 ms / 3,000 ms
コード長 564 bytes
コンパイル時間 130 ms
コンパイル使用メモリ 82,576 KB
実行使用メモリ 387,800 KB
最終ジャッジ日時 2024-09-17 05:21:55
合計ジャッジ時間 20,548 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
MOD = 10**9 + 7
inpl = lambda: list(map(int,input().split()))
N = int(input())
children = [set() for _ in range(N)]
parent = [None] * N
for _ in range(N-1):
    A, B = inpl()
    A, B = A-1, B-1
    children[A].add(B)
    parent[B] = A
depth=[-1]*N
root = parent.index(None)
ans = 0
pool = deque([root])
depth[root]=0
while pool:
    node = pool.popleft()
    ans += depth[node] * (depth[node]+1) // 2
    for c in children[node]:
        if depth[c]!=-1:continue
        depth[c]=depth[node]+1
        pool.append(c)

print(ans%MOD)
0