結果

問題 No.1103 Directed Length Sum
ユーザー Ldio03Ldio03
提出日時 2020-07-04 00:47:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,711 ms / 3,000 ms
コード長 690 bytes
コンパイル時間 211 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 222,704 KB
最終ジャッジ日時 2023-10-17 06:45:53
合計ジャッジ時間 15,345 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,452 KB
testcase_01 AC 42 ms
55,452 KB
testcase_02 AC 751 ms
218,568 KB
testcase_03 AC 554 ms
222,704 KB
testcase_04 AC 916 ms
134,860 KB
testcase_05 AC 1,711 ms
180,400 KB
testcase_06 AC 596 ms
114,440 KB
testcase_07 AC 169 ms
85,252 KB
testcase_08 AC 238 ms
89,796 KB
testcase_09 AC 128 ms
81,580 KB
testcase_10 AC 320 ms
95,496 KB
testcase_11 AC 1,007 ms
143,888 KB
testcase_12 AC 587 ms
114,484 KB
testcase_13 AC 309 ms
95,672 KB
testcase_14 AC 112 ms
79,860 KB
testcase_15 AC 470 ms
106,164 KB
testcase_16 AC 1,138 ms
147,636 KB
testcase_17 AC 1,191 ms
154,228 KB
testcase_18 AC 303 ms
95,296 KB
testcase_19 AC 1,024 ms
141,780 KB
testcase_20 AC 141 ms
83,132 KB
testcase_21 AC 209 ms
88,088 KB
testcase_22 AC 834 ms
129,072 KB
testcase_23 AC 502 ms
108,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import sys
input=sys.stdin.readline
MOD = 10**9 + 7
inpl = lambda: list(map(int,input().split()))
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