結果

問題 No.994 ばらばらコイン
ユーザー burita083burita083
提出日時 2021-03-14 18:25:47
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 491 ms / 2,000 ms
コード長 622 bytes
コンパイル時間 317 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 29,184 KB
最終ジャッジ日時 2024-04-24 01:16:13
合計ジャッジ時間 5,076 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
10,752 KB
testcase_01 AC 26 ms
10,752 KB
testcase_02 AC 26 ms
10,752 KB
testcase_03 AC 416 ms
29,184 KB
testcase_04 AC 382 ms
28,928 KB
testcase_05 AC 211 ms
20,096 KB
testcase_06 AC 491 ms
29,184 KB
testcase_07 AC 378 ms
29,056 KB
testcase_08 AC 26 ms
10,752 KB
testcase_09 AC 371 ms
27,904 KB
testcase_10 AC 286 ms
23,424 KB
testcase_11 AC 372 ms
25,088 KB
testcase_12 AC 345 ms
26,368 KB
testcase_13 AC 26 ms
10,752 KB
testcase_14 AC 26 ms
10,752 KB
testcase_15 AC 27 ms
10,880 KB
testcase_16 AC 25 ms
10,752 KB
testcase_17 AC 26 ms
10,880 KB
testcase_18 AC 25 ms
10,752 KB
testcase_19 AC 25 ms
10,880 KB
testcase_20 AC 24 ms
10,752 KB
testcase_21 AC 25 ms
10,752 KB
testcase_22 AC 25 ms
10,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
N, K = map(int, input().split())
if K > N:
  print(-1)
  exit()
graph = [[] for _ in range(N)]

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

queue = deque([0])
ans = [0] * N
dist = [-1] * N #Visitedとして使う


if K == 1:
  print(0)
  exit()
K -= 1
count = 0
while queue:
  v = queue.popleft()
  for e in graph[v]:
    if dist[e] != -1: #距離が確定確定してたらスルー
      continue
    dist[e] = dist[v] + 1
    K -= 1
    count += 1
    if K == 0:
      print(count)
      exit()
    queue.append(e)
print(-1)
0