結果

問題 No.994 ばらばらコイン
ユーザー RiburaRibura
提出日時 2020-05-01 01:27:42
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 287 ms / 2,000 ms
コード長 782 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 24,704 KB
最終ジャッジ日時 2024-12-21 09:16:42
合計ジャッジ時間 4,198 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

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