結果

問題 No.1103 Directed Length Sum
ユーザー AtamaokaCAtamaokaC
提出日時 2020-07-03 22:12:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,242 ms / 3,000 ms
コード長 563 bytes
コンパイル時間 475 ms
コンパイル使用メモリ 81,848 KB
実行使用メモリ 462,596 KB
最終ジャッジ日時 2023-10-17 04:01:50
合計ジャッジ時間 20,310 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
55,660 KB
testcase_01 AC 36 ms
55,660 KB
testcase_02 AC 1,444 ms
462,596 KB
testcase_03 AC 912 ms
276,208 KB
testcase_04 AC 1,217 ms
181,548 KB
testcase_05 AC 2,242 ms
256,004 KB
testcase_06 AC 792 ms
145,376 KB
testcase_07 AC 205 ms
92,896 KB
testcase_08 AC 300 ms
102,348 KB
testcase_09 AC 156 ms
86,776 KB
testcase_10 AC 398 ms
111,716 KB
testcase_11 AC 1,333 ms
190,764 KB
testcase_12 AC 759 ms
145,960 KB
testcase_13 AC 411 ms
112,620 KB
testcase_14 AC 143 ms
83,972 KB
testcase_15 AC 600 ms
130,784 KB
testcase_16 AC 1,533 ms
204,320 KB
testcase_17 AC 1,620 ms
209,888 KB
testcase_18 AC 381 ms
111,432 KB
testcase_19 AC 1,376 ms
193,532 KB
testcase_20 AC 175 ms
89,352 KB
testcase_21 AC 283 ms
100,304 KB
testcase_22 AC 1,125 ms
172,600 KB
testcase_23 AC 637 ms
134,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
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

layer = defaultdict(int)
forward, back = 0, 1
root = parent.index(None)
pool = [(root,0)]
while pool:
    node, d = pool.pop()
    layer[d] += 1
    for c in children[node]:
        pool.append((c, d+1))

ans = 0
for d, n in layer.items():
    ans += n*d*(d+1)//2
    ans %= MOD
print(ans)
0