結果

問題 No.994 ばらばらコイン
ユーザー kohei2019kohei2019
提出日時 2021-02-03 21:07:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 354 ms / 2,000 ms
コード長 691 bytes
コンパイル時間 289 ms
コンパイル使用メモリ 86,916 KB
実行使用メモリ 182,376 KB
最終ジャッジ日時 2023-09-12 16:49:37
合計ジャッジ時間 6,018 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,700 KB
testcase_01 AC 93 ms
71,532 KB
testcase_02 AC 354 ms
182,376 KB
testcase_03 AC 272 ms
91,120 KB
testcase_04 AC 270 ms
91,100 KB
testcase_05 AC 187 ms
84,668 KB
testcase_06 AC 277 ms
91,400 KB
testcase_07 AC 271 ms
91,244 KB
testcase_08 AC 226 ms
87,060 KB
testcase_09 AC 258 ms
90,344 KB
testcase_10 AC 218 ms
87,300 KB
testcase_11 AC 233 ms
88,396 KB
testcase_12 AC 248 ms
89,328 KB
testcase_13 AC 92 ms
71,532 KB
testcase_14 AC 92 ms
71,576 KB
testcase_15 AC 94 ms
71,564 KB
testcase_16 AC 93 ms
71,568 KB
testcase_17 AC 93 ms
71,720 KB
testcase_18 AC 93 ms
71,332 KB
testcase_19 AC 92 ms
71,656 KB
testcase_20 AC 93 ms
71,680 KB
testcase_21 AC 93 ms
71,332 KB
testcase_22 AC 93 ms
71,412 KB
権限があれば一括ダウンロードができます

ソースコード

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