結果

問題 No.1103 Directed Length Sum
ユーザー Ldio03Ldio03
提出日時 2020-07-04 00:41:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,868 ms / 3,000 ms
コード長 564 bytes
コンパイル時間 172 ms
コンパイル使用メモリ 81,564 KB
実行使用メモリ 386,712 KB
最終ジャッジ日時 2023-10-17 06:44:11
合計ジャッジ時間 23,570 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,452 KB
testcase_01 AC 42 ms
55,452 KB
testcase_02 AC 1,545 ms
386,712 KB
testcase_03 AC 973 ms
268,452 KB
testcase_04 AC 1,431 ms
191,344 KB
testcase_05 AC 2,868 ms
278,900 KB
testcase_06 AC 896 ms
151,076 KB
testcase_07 AC 263 ms
93,256 KB
testcase_08 AC 372 ms
103,556 KB
testcase_09 AC 192 ms
86,864 KB
testcase_10 AC 485 ms
113,344 KB
testcase_11 AC 1,565 ms
199,476 KB
testcase_12 AC 928 ms
151,400 KB
testcase_13 AC 501 ms
114,144 KB
testcase_14 AC 168 ms
83,968 KB
testcase_15 AC 731 ms
135,224 KB
testcase_16 AC 1,803 ms
215,916 KB
testcase_17 AC 1,916 ms
221,852 KB
testcase_18 AC 491 ms
113,216 KB
testcase_19 AC 1,602 ms
204,508 KB
testcase_20 AC 221 ms
89,488 KB
testcase_21 AC 343 ms
101,264 KB
testcase_22 AC 1,304 ms
181,644 KB
testcase_23 AC 783 ms
137,468 KB
権限があれば一括ダウンロードができます

ソースコード

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