結果

問題 No.1103 Directed Length Sum
ユーザー Ldio03Ldio03
提出日時 2020-07-04 00:43:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,520 ms / 3,000 ms
コード長 558 bytes
コンパイル時間 184 ms
コンパイル使用メモリ 81,568 KB
実行使用メモリ 386,992 KB
最終ジャッジ日時 2023-10-17 06:45:19
合計ジャッジ時間 21,287 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,452 KB
testcase_01 AC 42 ms
55,452 KB
testcase_02 AC 1,386 ms
386,992 KB
testcase_03 AC 836 ms
274,656 KB
testcase_04 AC 1,277 ms
191,240 KB
testcase_05 AC 2,520 ms
279,020 KB
testcase_06 AC 817 ms
150,540 KB
testcase_07 AC 236 ms
93,500 KB
testcase_08 AC 335 ms
102,888 KB
testcase_09 AC 176 ms
87,252 KB
testcase_10 AC 443 ms
113,524 KB
testcase_11 AC 1,393 ms
201,228 KB
testcase_12 AC 831 ms
151,152 KB
testcase_13 AC 448 ms
114,200 KB
testcase_14 AC 155 ms
83,808 KB
testcase_15 AC 660 ms
133,356 KB
testcase_16 AC 1,584 ms
216,548 KB
testcase_17 AC 1,692 ms
222,156 KB
testcase_18 AC 441 ms
113,276 KB
testcase_19 AC 1,439 ms
204,468 KB
testcase_20 AC 198 ms
89,040 KB
testcase_21 AC 309 ms
99,816 KB
testcase_22 AC 1,161 ms
180,024 KB
testcase_23 AC 707 ms
139,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
MOD = 10**9 + 7
inpl = lambda: 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