結果

問題 No.1103 Directed Length Sum
ユーザー 👑 rin204rin204
提出日時 2020-08-13 07:51:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,453 ms / 3,000 ms
コード長 553 bytes
コンパイル時間 192 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 236,684 KB
最終ジャッジ日時 2024-04-18 01:18:43
合計ジャッジ時間 13,972 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,632 KB
testcase_01 AC 34 ms
53,540 KB
testcase_02 AC 686 ms
200,480 KB
testcase_03 AC 596 ms
236,684 KB
testcase_04 AC 803 ms
134,528 KB
testcase_05 AC 1,453 ms
178,048 KB
testcase_06 AC 521 ms
110,720 KB
testcase_07 AC 148 ms
84,736 KB
testcase_08 AC 212 ms
88,624 KB
testcase_09 AC 115 ms
81,200 KB
testcase_10 AC 267 ms
93,696 KB
testcase_11 AC 887 ms
138,812 KB
testcase_12 AC 507 ms
110,536 KB
testcase_13 AC 283 ms
93,952 KB
testcase_14 AC 104 ms
80,304 KB
testcase_15 AC 405 ms
102,656 KB
testcase_16 AC 1,005 ms
145,920 KB
testcase_17 AC 1,084 ms
148,224 KB
testcase_18 AC 274 ms
93,568 KB
testcase_19 AC 906 ms
140,800 KB
testcase_20 AC 125 ms
82,772 KB
testcase_21 AC 180 ms
88,064 KB
testcase_22 AC 776 ms
128,140 KB
testcase_23 AC 441 ms
105,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
MOD = 10 ** 9 + 7

n = int(input())
root_cand = [True for _ in range(n)]
edges = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    edges[a - 1].append(b - 1)
    root_cand[b - 1] = False
root = root_cand.index(True)

ans = 0
queue = deque()
queue.append(root)
queue.append(0)
while queue:
    pos = queue.popleft()
    d = queue.popleft()
    ans += d * (d + 1) // 2
    ans %= MOD
    for n_pos in edges[pos]:
        queue.append(n_pos)
        queue.append(d + 1)
print(ans)
        
0