結果

問題 No.994 ばらばらコイン
ユーザー toshiro_yanagitoshiro_yanagi
提出日時 2020-07-19 12:36:29
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 680 bytes
コンパイル時間 474 ms
コンパイル使用メモリ 10,728 KB
実行使用メモリ 55,504 KB
最終ジャッジ日時 2023-08-22 09:51:35
合計ジャッジ時間 15,893 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 1,184 ms
54,728 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 956 ms
38,700 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 22 ms
8,960 KB
testcase_17 AC 22 ms
9,060 KB
testcase_18 AC 22 ms
8,972 KB
testcase_19 AC 22 ms
9,048 KB
testcase_20 AC 21 ms
9,104 KB
testcase_21 WA -
testcase_22 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import queue
from collections import namedtuple
P = namedtuple("P", "i c")

n, k = map(int, input().split())
path = {}
cst = {}

for i in range(1, n + 1):
    path[i] = set()
    cst[i] = -1

for _ in range(n - 1):
    a, b = map(int, input().split())
    path[a] |= {b}
    path[b] |= {a}

q = queue.Queue()
q.put(P(1, 0))
while q.qsize() != 0:
    p = q.get()
    if cst[p.i] >= 0 and cst[p.i] < p.c: continue
    cst[p.i] = p.c

    for i in path[p.i]:
        q.put(P(i, p.c + 1))

lst = list()
for v in cst.values():
    lst.append(v)

lst.sort()
lst.reverse()

ans = 0
for _ in range(k):
    if lst == list():
        ans = -1
        break
    ans += lst.pop()

print(ans)
0