結果

問題 No.994 ばらばらコイン
ユーザー brthyyjpbrthyyjp
提出日時 2020-02-21 22:00:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 156 ms / 2,000 ms
コード長 673 bytes
コンパイル時間 201 ms
コンパイル使用メモリ 82,580 KB
実行使用メモリ 89,216 KB
最終ジャッジ日時 2024-04-17 07:37:00
合計ジャッジ時間 2,751 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,376 KB
testcase_01 AC 40 ms
53,504 KB
testcase_02 AC 85 ms
84,348 KB
testcase_03 AC 113 ms
85,632 KB
testcase_04 AC 108 ms
85,504 KB
testcase_05 AC 85 ms
80,000 KB
testcase_06 AC 156 ms
89,216 KB
testcase_07 AC 107 ms
85,376 KB
testcase_08 AC 82 ms
80,128 KB
testcase_09 AC 107 ms
85,376 KB
testcase_10 AC 94 ms
84,096 KB
testcase_11 AC 126 ms
84,916 KB
testcase_12 AC 114 ms
85,504 KB
testcase_13 AC 40 ms
53,376 KB
testcase_14 AC 42 ms
54,144 KB
testcase_15 AC 40 ms
53,376 KB
testcase_16 AC 38 ms
51,840 KB
testcase_17 AC 40 ms
53,504 KB
testcase_18 AC 36 ms
52,332 KB
testcase_19 AC 37 ms
52,352 KB
testcase_20 AC 37 ms
51,840 KB
testcase_21 AC 39 ms
53,248 KB
testcase_22 AC 41 ms
53,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

n, k = map(int, input().split())
g = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    g[a].append(b)
    g[b].append(a)
#print(g)

if k > n:
    print(-1)
    exit()

if k == 1:
    print(0)
    exit()

from collections import deque
q = deque()
q.append(0)
visit = [0]*n
cnt = 0
visit[0] = 1
while q:
    x = q.popleft()
    for new_x in g[x]:
        if visit[new_x] == 0:
            cnt += 1
            visit[new_x] = 1
            q.append(new_x)
        if cnt == k-1:
            print(sum(visit)-1)
            #print(visit)
            exit()
#print(cnt)
#print(visit)
0