結果

問題 No.994 ばらばらコイン
ユーザー maspy
提出日時 2020-02-27 01:41:16
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 692 bytes
コンパイル時間 80 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 40,348 KB
最終ジャッジ日時 2024-10-13 15:39:43
合計ジャッジ時間 4,101 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 7 WA * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
# %%
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


# %%
N, K = map(int, readline().split())
m = map(int, read().split())
AB = tuple(zip(m, m))

# %%
graph = [[] for _ in range(N + 1)]
for a, b in AB:
    graph[a].append(b)
    graph[b].append(a)

# %%
dist = [0] * (N + 1)
parent = [0] * (N + 1)
stack = [1]
while stack:
    v = stack.pop()
    for w in graph[v]:
        if w == parent[v]:
            continue
        parent[w] = v
        stack.append(w)
        dist[w] = dist[v] + 1

# %%
dist = sorted(dist[1:])

# %%
if K > N:
    answer = -1
else:
    answer = sum(dist[:K])
print(answer)
0