結果

問題 No.994 ばらばらコイン
ユーザー tobusakanatobusakana
提出日時 2022-11-27 14:48:28
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 706 bytes
コンパイル時間 202 ms
コンパイル使用メモリ 82,360 KB
実行使用メモリ 88,540 KB
最終ジャッジ日時 2024-04-14 22:42:08
合計ジャッジ時間 3,553 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 36 ms
52,616 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 38 ms
53,440 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 38 ms
52,372 KB
testcase_17 AC 42 ms
54,500 KB
testcase_18 AC 38 ms
52,436 KB
testcase_19 AC 41 ms
53,756 KB
testcase_20 AC 37 ms
52,324 KB
testcase_21 WA -
testcase_22 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline = sys.stdin.readline
N,K = map(int,readline().split())
if N < K:
    print(-1)
    exit(0)
    
G = [[] for i in range(N)]
for _ in range(N - 1):
    a,b = map(int,readline().split())
    G[a - 1].append(b - 1)
    G[b - 1].append(a - 1)
# bfsで周辺の頂点を見ていき、初めて見た場所であれば距離をansに加算
# 追加した頂点数がKになったら終了

from collections import deque
q = deque([])
q.append([0, 0, -1])
ans = 0
while q:
    v, d, p = q.popleft()
    ans += d
    K -= 1
    if K == 0:
        print(ans)
        break
    for child in G[v]:
        if child == p:
            continue
        q.append([child, d + 1, v])
    
    
    
    
0