結果

問題 No.1103 Directed Length Sum
ユーザー AtamaokaCAtamaokaC
提出日時 2020-07-04 00:54:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,771 ms / 3,000 ms
コード長 713 bytes
コンパイル時間 251 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 229,948 KB
最終ジャッジ日時 2023-10-17 06:46:19
合計ジャッジ時間 16,072 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,604 KB
testcase_01 AC 42 ms
55,604 KB
testcase_02 AC 783 ms
220,188 KB
testcase_03 AC 599 ms
229,948 KB
testcase_04 AC 941 ms
138,600 KB
testcase_05 AC 1,771 ms
183,192 KB
testcase_06 AC 608 ms
114,644 KB
testcase_07 AC 172 ms
85,216 KB
testcase_08 AC 245 ms
90,268 KB
testcase_09 AC 131 ms
81,636 KB
testcase_10 AC 323 ms
95,072 KB
testcase_11 AC 1,033 ms
140,344 KB
testcase_12 AC 620 ms
114,676 KB
testcase_13 AC 336 ms
95,296 KB
testcase_14 AC 118 ms
79,976 KB
testcase_15 AC 497 ms
106,368 KB
testcase_16 AC 1,215 ms
147,924 KB
testcase_17 AC 1,264 ms
154,560 KB
testcase_18 AC 326 ms
95,024 KB
testcase_19 AC 1,065 ms
142,072 KB
testcase_20 AC 151 ms
83,164 KB
testcase_21 AC 233 ms
88,908 KB
testcase_22 AC 868 ms
129,896 KB
testcase_23 AC 529 ms
108,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import sys
input = lambda: sys.stdin.readline().rstrip()
inpl = lambda: list(map(int,input().split()))
MOD = 10**9 + 7
N = int(input())
children = [[] for _ in range(N)]
parent = [None] * N
for _ in range(N-1):
    A, B = inpl()
    A, B = A-1, B-1
    children[A].append(B)
    parent[B] = A
root = parent.index(None)

pool = deque([root, -1])
ans = 0
d, n = 0, 0
while pool:
    node = pool.popleft()
    if node < 0:
        ans += (n * d * (d+1) // 2) % MOD
        ans %= MOD
        n = 0
        d += 1
        if pool:
            pool.append(-1)
        else:
            break
    else:
        n += 1
        for c in children[node]:
            pool.append(c)
print(ans)
0