結果

問題 No.994 ばらばらコイン
ユーザー RiburaRibura
提出日時 2020-05-01 01:27:42
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 223 ms / 2,000 ms
コード長 782 bytes
コンパイル時間 97 ms
コンパイル使用メモリ 10,880 KB
実行使用メモリ 22,532 KB
最終ジャッジ日時 2023-08-23 08:10:47
合計ジャッジ時間 3,347 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,480 KB
testcase_01 AC 17 ms
8,636 KB
testcase_02 AC 184 ms
22,532 KB
testcase_03 AC 164 ms
21,328 KB
testcase_04 AC 160 ms
21,232 KB
testcase_05 AC 90 ms
14,724 KB
testcase_06 AC 223 ms
21,428 KB
testcase_07 AC 160 ms
21,256 KB
testcase_08 AC 145 ms
17,300 KB
testcase_09 AC 153 ms
20,592 KB
testcase_10 AC 116 ms
17,312 KB
testcase_11 AC 158 ms
18,440 KB
testcase_12 AC 150 ms
19,176 KB
testcase_13 AC 18 ms
8,572 KB
testcase_14 AC 18 ms
8,620 KB
testcase_15 AC 18 ms
8,668 KB
testcase_16 AC 18 ms
8,668 KB
testcase_17 AC 18 ms
8,492 KB
testcase_18 AC 18 ms
8,620 KB
testcase_19 AC 17 ms
8,584 KB
testcase_20 AC 18 ms
8,604 KB
testcase_21 AC 18 ms
8,604 KB
testcase_22 AC 18 ms
8,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from collections import deque

n, k = map(int, readline().split())
graph = [[] for _ in range(n)]
for i in range(n - 1):
    a, b = map(int, readline().split())
    graph[a - 1].append(b - 1)
if k == 1:
    print(0)
    exit()


def dfs(s):
    global k
    cnt = 0
    check = [False] * n
    stack = deque([s])
    while stack:
        now = stack.pop()
        check[now] = True
        for next in graph[now]:
            if check[next]:
                continue
            k -= 1
            cnt += 1
            if k == 1:
                print(cnt)
                exit()
            stack.appendleft(next)


dfs(0)
print(-1)
0