結果

問題 No.994 ばらばらコイン
ユーザー toshiro_yanagi
提出日時 2020-07-19 12:36:29
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 680 bytes
コンパイル時間 299 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 57,708 KB
最終ジャッジ日時 2024-12-16 05:42:14
合計ジャッジ時間 16,961 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 7 WA * 16
権限があれば一括ダウンロードができます

ソースコード

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