結果

問題 No.994 ばらばらコイン
ユーザー neko_the_shadow
提出日時 2020-02-21 23:04:06
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 951 bytes
コンパイル時間 570 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 41,712 KB
最終ジャッジ日時 2024-10-09 01:05:33
合計ジャッジ時間 7,115 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 7 WA * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections, sys

sys.setrecursionlimit(1000000)

n, k = map(int, input().split())

nxts = collections.defaultdict(list)
for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    nxts[a].append(b)
    nxts[b].append(a)

if n < k:
    print(-1)
    exit(0)

childs = {}
def f(parent, current):
    if current in childs:
        return childs[current]
    
    v = 1
    for nxt in nxts[current]:
        if nxt != parent:
            v += f(current, nxt)
    childs[current] = v
    return v
f(-1, 0)

def g(parent, current, coin):
    if coin <= len(nxts[current]):
        return 1

    ans = 1
    for nxt in sorted(filter(lambda nxt: nxt != parent, nxts[current]), key=lambda child: len(nxts[child]), reverse=True):
        if coin <= childs[nxt]:
            ans += g(current, nxt, coin)
            break
        ans += g(current, nxt, childs[nxt])
        coin -= childs[nxt]
    return ans

print(g(-1, 0, k))
0