結果

問題 No.1103 Directed Length Sum
ユーザー rin204rin204
提出日時 2020-08-13 07:50:24
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 538 bytes
コンパイル時間 307 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 236,428 KB
最終ジャッジ日時 2024-04-18 01:17:22
合計ジャッジ時間 20,329 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
53,376 KB
testcase_01 AC 48 ms
53,376 KB
testcase_02 WA -
testcase_03 AC 769 ms
236,428 KB
testcase_04 AC 1,113 ms
134,400 KB
testcase_05 AC 1,978 ms
177,664 KB
testcase_06 AC 719 ms
110,592 KB
testcase_07 AC 223 ms
83,968 KB
testcase_08 AC 312 ms
88,576 KB
testcase_09 AC 178 ms
81,408 KB
testcase_10 AC 404 ms
93,568 KB
testcase_11 AC 1,172 ms
138,240 KB
testcase_12 AC 694 ms
110,848 KB
testcase_13 AC 374 ms
93,696 KB
testcase_14 AC 152 ms
80,512 KB
testcase_15 AC 564 ms
102,528 KB
testcase_16 AC 1,372 ms
145,920 KB
testcase_17 AC 1,434 ms
148,224 KB
testcase_18 AC 387 ms
93,696 KB
testcase_19 AC 1,232 ms
140,544 KB
testcase_20 AC 186 ms
82,944 KB
testcase_21 AC 278 ms
88,064 KB
testcase_22 AC 982 ms
128,256 KB
testcase_23 AC 604 ms
105,344 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
    for n_pos in edges[pos]:
        queue.append(n_pos)
        queue.append(d + 1)
print(ans)
        
0