結果

問題 No.196 典型DP (1)
ユーザー Mao-betaMao-beta
提出日時 2024-04-04 02:17:35
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,274 ms / 2,000 ms
コード長 1,963 bytes
コンパイル時間 212 ms
コンパイル使用メモリ 12,032 KB
実行使用メモリ 64,128 KB
最終ジャッジ日時 2024-04-04 02:18:02
合計ジャッジ時間 26,012 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,368 KB
testcase_01 AC 29 ms
10,368 KB
testcase_02 AC 29 ms
10,368 KB
testcase_03 AC 29 ms
10,368 KB
testcase_04 AC 30 ms
10,368 KB
testcase_05 AC 29 ms
10,368 KB
testcase_06 AC 28 ms
10,368 KB
testcase_07 AC 28 ms
10,368 KB
testcase_08 AC 29 ms
10,368 KB
testcase_09 AC 29 ms
10,368 KB
testcase_10 AC 29 ms
10,368 KB
testcase_11 AC 29 ms
10,368 KB
testcase_12 AC 29 ms
10,368 KB
testcase_13 AC 30 ms
10,368 KB
testcase_14 AC 31 ms
10,368 KB
testcase_15 AC 58 ms
11,520 KB
testcase_16 AC 117 ms
14,592 KB
testcase_17 AC 233 ms
19,968 KB
testcase_18 AC 408 ms
28,160 KB
testcase_19 AC 533 ms
32,896 KB
testcase_20 AC 743 ms
42,880 KB
testcase_21 AC 756 ms
43,136 KB
testcase_22 AC 742 ms
43,264 KB
testcase_23 AC 917 ms
64,128 KB
testcase_24 AC 884 ms
57,472 KB
testcase_25 AC 869 ms
55,552 KB
testcase_26 AC 1,039 ms
42,880 KB
testcase_27 AC 1,016 ms
42,880 KB
testcase_28 AC 1,042 ms
42,880 KB
testcase_29 AC 1,051 ms
42,880 KB
testcase_30 AC 1,041 ms
42,880 KB
testcase_31 AC 1,268 ms
42,368 KB
testcase_32 AC 1,269 ms
42,368 KB
testcase_33 AC 1,268 ms
42,368 KB
testcase_34 AC 1,274 ms
42,368 KB
testcase_35 AC 1,269 ms
42,368 KB
testcase_36 AC 1,220 ms
42,368 KB
testcase_37 AC 776 ms
42,496 KB
testcase_38 AC 1,228 ms
42,368 KB
testcase_39 AC 1,147 ms
42,368 KB
testcase_40 AC 1,255 ms
42,368 KB
testcase_41 AC 29 ms
10,368 KB
testcase_42 AC 29 ms
10,368 KB
testcase_43 AC 29 ms
10,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math
import bisect
from heapq import heapify, heappop, heappush
from collections import deque, defaultdict, Counter
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
MOD99 = 998244353

input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()
SMI = lambda: input().split()
SLI = lambda: list(SMI())
EI = lambda m: [NLI() for _ in range(m)]


def adjlist(n, edges, directed=False, in_origin=1):
    if len(edges) == 0:
        return [[] for _ in range(n)]

    weighted = True if len(edges[0]) > 2 else False
    if in_origin == 1:
        if weighted:
            edges = [[x-1, y-1, w] for x, y, w in edges]
        else:
            edges = [[x-1, y-1] for x, y in edges]

    res = [[] for _ in range(n)]

    if weighted:
        for u, v, c in edges:
            res[u].append([v, c])
            if not directed:
                res[v].append([u, c])

    else:
        for u, v in edges:
            res[u].append(v)
            if not directed:
                res[v].append(u)

    return res


def main():
    N, K = NMI()
    AB = EI(N-1)
    # i以下の部分木でj個塗る
    dp = [[0]*(N+1) for _ in range(N)]
    P = [N] * N
    G = adjlist(N, AB, in_origin=0)
    # i以下の部分木のサイズ
    SZ = [0] * N

    def dfs(now):
        SZ[now] = 1
        dp[now][0] = 1
        for v in G[now]:
            if v == P[now]:
                continue
            P[v] = now
            dfs(v)
            for i in range(SZ[now], -1, -1):
                for j in range(SZ[v], 0, -1):
                    dp[now][i+j] += dp[now][i] * dp[v][j] % MOD
                    dp[now][i+j] %= MOD
            SZ[now] += SZ[v]
        dp[now][SZ[now]] = 1

    dfs(0)
    print(dp[0][K])


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