結果

問題 No.994 ばらばらコイン
ユーザー stngstng
提出日時 2022-08-17 14:28:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 195 ms / 2,000 ms
コード長 647 bytes
コンパイル時間 198 ms
コンパイル使用メモリ 82,408 KB
実行使用メモリ 89,120 KB
最終ジャッジ日時 2024-04-15 04:33:57
合計ジャッジ時間 3,269 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
55,760 KB
testcase_01 AC 39 ms
54,776 KB
testcase_02 AC 134 ms
88,628 KB
testcase_03 AC 172 ms
89,120 KB
testcase_04 AC 154 ms
88,660 KB
testcase_05 AC 111 ms
82,232 KB
testcase_06 AC 195 ms
89,004 KB
testcase_07 AC 153 ms
88,776 KB
testcase_08 AC 148 ms
85,424 KB
testcase_09 AC 149 ms
86,064 KB
testcase_10 AC 115 ms
82,752 KB
testcase_11 AC 166 ms
85,704 KB
testcase_12 AC 152 ms
85,968 KB
testcase_13 AC 38 ms
54,188 KB
testcase_14 AC 39 ms
54,368 KB
testcase_15 AC 38 ms
55,376 KB
testcase_16 AC 37 ms
53,788 KB
testcase_17 AC 39 ms
54,224 KB
testcase_18 AC 38 ms
54,856 KB
testcase_19 AC 38 ms
54,384 KB
testcase_20 AC 39 ms
54,196 KB
testcase_21 AC 38 ms
53,696 KB
testcase_22 AC 38 ms
55,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

n,k = map(int,input().split())

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

d = deque()
d.append([0,0])
chk = [0]*(n)
chk[0] = 1

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

f = False
cnt = 0

while f == False and len(d) > 0:
    tmp,num = d.popleft()
    for i in ab[tmp]:
        if chk[i] == 0:
            chk[i] = 1
            d.append([i,num+1])
            k -= 1
            cnt += 1
            if k == 1:
                f = True
                break
#print(k)
if f == True:
    print(cnt)
else:
    print(-1)
0