結果

問題 No.994 ばらばらコイン
コンテスト
ユーザー 👑 rin204
提出日時 2020-08-20 11:23:17
言語 Python3
(3.14.7 + numpy 2.5.2 + scipy 1.18.0 + ACL)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
WA  
実行時間 -
コード長 651 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 56 ms
コンパイル使用メモリ 15,232 KB
実行使用メモリ 28,544 KB
最終ジャッジ日時 2026-09-15 09:57:04
合計ジャッジ時間 4,303 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 7 WA * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque

n, k = map(int, input().split())
if n < k:
    print(-1)
    exit()

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

queue = deque()
queue.append(0)
queue.append(0)
ans = 0
check = [False for _ in range(n)]
check[0] = True
while queue:
    if k == 0:
        break
    pos = queue.popleft()
    cnt = queue.popleft()
    ans += cnt
    k -= 1
    for n_pos in edges[pos]:
        if check[n_pos]:
            continue
        check[n_pos] = True
        queue.append(n_pos)
        queue.append(cnt + 1)
print(ans)
0