結果

問題 No.994 ばらばらコイン
ユーザー kohei2019
提出日時 2021-02-03 21:07:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 307 ms / 2,000 ms
コード長 691 bytes
コンパイル時間 163 ms
コンパイル使用メモリ 82,000 KB
実行使用メモリ 170,640 KB
最終ジャッジ日時 2024-06-30 04:43:51
合計ジャッジ時間 4,225 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections
import sys
sys.setrecursionlimit(10**7)
N,K = map(int,input().split())
lsg = [[] for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    lsg[a].append(b)
    lsg[b].append(a)
#できるかどうかの判定が必要
depth = [-1]*(N)
depth[0] = 0
d = collections.deque()
d.append(0)
while d:
    v = d.pop()
    for j in lsg[v]:
        if depth[j] >= 0:
            continue
        depth[j] = depth[v]+1
        d.append(j)
lsc = [-1]*(N)
def dfs(k):
    cnt = 1
    for j in lsg[k]:
        if depth[j] < depth[k]:
            continue
        cnt += dfs(j)
    lsc[k] = cnt
    return cnt
dfs(0)

print(-1 if N < K else K-1)
0