結果

問題 No.994 ばらばらコイン
ユーザー neko_the_shadowneko_the_shadow
提出日時 2020-02-21 23:04:06
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 951 bytes
コンパイル時間 1,039 ms
コンパイル使用メモリ 10,992 KB
実行使用メモリ 39,552 KB
最終ジャッジ日時 2023-07-30 07:28:43
合計ジャッジ時間 7,582 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,600 KB
testcase_01 WA -
testcase_02 AC 375 ms
29,872 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 271 ms
21,908 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 19 ms
8,672 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 19 ms
8,520 KB
testcase_17 WA -
testcase_18 AC 19 ms
8,684 KB
testcase_19 WA -
testcase_20 AC 18 ms
8,516 KB
testcase_21 WA -
testcase_22 WA -
権限があれば一括ダウンロードができます

ソースコード

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