結果

問題 No.1103 Directed Length Sum
ユーザー qumazakiqumazaki
提出日時 2020-07-04 02:30:54
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 742 bytes
コンパイル時間 184 ms
コンパイル使用メモリ 81,460 KB
実行使用メモリ 358,224 KB
最終ジャッジ日時 2023-10-17 08:28:25
合計ジャッジ時間 17,084 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,228 KB
testcase_01 AC 39 ms
53,228 KB
testcase_02 WA -
testcase_03 AC 608 ms
353,496 KB
testcase_04 AC 1,051 ms
264,808 KB
testcase_05 AC 2,009 ms
358,224 KB
testcase_06 AC 600 ms
196,124 KB
testcase_07 AC 149 ms
109,140 KB
testcase_08 AC 214 ms
126,248 KB
testcase_09 AC 112 ms
96,056 KB
testcase_10 AC 341 ms
137,460 KB
testcase_11 AC 1,066 ms
267,148 KB
testcase_12 AC 685 ms
196,512 KB
testcase_13 AC 341 ms
143,768 KB
testcase_14 AC 98 ms
92,000 KB
testcase_15 AC 506 ms
170,472 KB
testcase_16 AC 1,305 ms
280,620 KB
testcase_17 AC 1,426 ms
300,144 KB
testcase_18 AC 328 ms
137,688 KB
testcase_19 AC 1,099 ms
267,388 KB
testcase_20 AC 125 ms
101,536 KB
testcase_21 AC 204 ms
121,556 KB
testcase_22 AC 890 ms
245,348 KB
testcase_23 AC 531 ms
181,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
ab = list(map(int,read().split()))

it = iter(ab)
root = (n+1)*n//2
parent = [0] * (n+1)
links = [[] for _ in range(n+1)]
for a,b in zip(it,it):
    links[a].append(b)
    parent[b] = a
    root -= b

tp = [root]
stack = [root]
while(stack):
    next = []
    while(stack):
        i = stack.pop()
        for j in links[i]:
            next.append(j)
            tp.append(j)
    stack = next[::]

score = [[0,0] for _ in range(n+1)]

ans = 0
for i in tp[::-1]:
    ans += score[i][0]
    par = parent[i]
    score[par][0] += score[i][0] + score[i][1] + 1
    score[par][1] += score[i][1] + 1

print(ans)
0