結果

問題 No.994 ばらばらコイン
ユーザー ntk-ta01ntk-ta01
提出日時 2020-02-21 21:43:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 276 ms / 2,000 ms
コード長 853 bytes
コンパイル時間 307 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 89,472 KB
最終ジャッジ日時 2024-04-17 07:32:05
合計ジャッジ時間 4,014 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
53,888 KB
testcase_01 AC 55 ms
54,144 KB
testcase_02 AC 173 ms
86,784 KB
testcase_03 AC 222 ms
88,064 KB
testcase_04 AC 220 ms
87,552 KB
testcase_05 AC 163 ms
82,432 KB
testcase_06 AC 276 ms
89,472 KB
testcase_07 AC 219 ms
87,680 KB
testcase_08 AC 177 ms
84,096 KB
testcase_09 AC 213 ms
87,424 KB
testcase_10 AC 181 ms
84,864 KB
testcase_11 AC 220 ms
84,992 KB
testcase_12 AC 214 ms
87,040 KB
testcase_13 AC 49 ms
53,632 KB
testcase_14 AC 49 ms
53,632 KB
testcase_15 AC 49 ms
53,248 KB
testcase_16 AC 48 ms
53,632 KB
testcase_17 AC 49 ms
53,120 KB
testcase_18 AC 48 ms
53,760 KB
testcase_19 AC 48 ms
53,760 KB
testcase_20 AC 49 ms
53,888 KB
testcase_21 AC 48 ms
53,248 KB
testcase_22 AC 48 ms
53,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    N, K = (int(i) for i in input().split())
    G = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = (int(i)-1 for i in input().split())
        G[a].append(b)
        G[b].append(a)
    from collections import deque

    def bfs(s, K):
        dist = [-1 for _ in range(len(G))]
        que = deque()
        dist[s] = 0
        K -= 1
        ans = 0
        que.append(s)
        while que and K > 0:
            u = que.popleft()
            for v in G[u]:
                if dist[v] != -1:
                    continue
                dist[v] = dist[u] + 1
                K -= 1
                ans += 1
                que.append(v)
                if K == 0:
                    break
        return ans
    if N < K:
        return print(-1)
    ans = bfs(0, K)
    print(ans)


if __name__ == '__main__':
    main()
0