結果

問題 No.196 典型DP (1)
ユーザー mkawa2mkawa2
提出日時 2023-01-12 11:47:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 186 ms / 2,000 ms
コード長 1,875 bytes
コンパイル時間 399 ms
コンパイル使用メモリ 86,648 KB
実行使用メモリ 87,720 KB
最終ジャッジ日時 2023-08-24 19:54:49
合計ジャッジ時間 6,752 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,060 KB
testcase_01 AC 70 ms
70,916 KB
testcase_02 AC 72 ms
71,144 KB
testcase_03 AC 72 ms
71,104 KB
testcase_04 AC 71 ms
71,380 KB
testcase_05 AC 71 ms
71,056 KB
testcase_06 AC 72 ms
71,304 KB
testcase_07 AC 72 ms
71,164 KB
testcase_08 AC 72 ms
71,100 KB
testcase_09 AC 73 ms
71,248 KB
testcase_10 AC 73 ms
71,148 KB
testcase_11 AC 72 ms
71,100 KB
testcase_12 AC 76 ms
75,572 KB
testcase_13 AC 79 ms
76,084 KB
testcase_14 AC 79 ms
75,928 KB
testcase_15 AC 81 ms
75,864 KB
testcase_16 AC 94 ms
76,124 KB
testcase_17 AC 110 ms
77,700 KB
testcase_18 AC 123 ms
77,888 KB
testcase_19 AC 123 ms
77,712 KB
testcase_20 AC 122 ms
77,896 KB
testcase_21 AC 122 ms
77,876 KB
testcase_22 AC 121 ms
77,580 KB
testcase_23 AC 137 ms
81,316 KB
testcase_24 AC 128 ms
77,860 KB
testcase_25 AC 138 ms
80,968 KB
testcase_26 AC 108 ms
77,896 KB
testcase_27 AC 116 ms
77,896 KB
testcase_28 AC 127 ms
77,916 KB
testcase_29 AC 142 ms
87,720 KB
testcase_30 AC 155 ms
87,020 KB
testcase_31 AC 104 ms
78,212 KB
testcase_32 AC 150 ms
77,908 KB
testcase_33 AC 166 ms
78,052 KB
testcase_34 AC 186 ms
78,348 KB
testcase_35 AC 182 ms
78,044 KB
testcase_36 AC 106 ms
78,136 KB
testcase_37 AC 127 ms
77,892 KB
testcase_38 AC 168 ms
78,300 KB
testcase_39 AC 170 ms
78,112 KB
testcase_40 AC 180 ms
78,092 KB
testcase_41 AC 70 ms
71,092 KB
testcase_42 AC 71 ms
70,996 KB
testcase_43 AC 71 ms
70,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(1000005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# inf = (1 << 31)-1
md = 10**9+7
# md = 998244353

n, k = LI()
to = [[] for _ in range(n)]
for _ in range(n-1):
    u, v = LI()
    to[u].append(v)
    to[v].append(u)

parent, depth, vis = [-1]*n, [0]*n, [0]*n

def dfs(root):
    uu = []
    vis[root] = 1
    stack = [root]
    while stack:
        iu = stack.pop()
        i, u = divmod(iu, n)
        if i == 0:
            uu.append(u)
        # care to's format
        while i < len(to[u]) and vis[to[u][i]]: i += 1
        if i < len(to[u]):
            stack.append((i+1)*n+u)
            # care to's format
            v = to[u][i]
            vis[v] = 1
            stack.append(v)
            parent[v] = u
            depth[v] = depth[u]+1

    return uu

uu = dfs(0)
size = [1]*n
for u in uu[:0:-1]:
    p = parent[u]
    size[p] += size[u]

def merge(p, u):
    m = min(k+1, len(dp[p])+len(dp[u])-1)
    cur = [0]*m
    for s, c in enumerate(dp[p]):
        for t, d in enumerate(dp[u]):
            if s+t >= m: break
            cur[s+t] += c*d%md
            cur[s+t] %= md
    dp[p] = cur

dp = [[1] for _ in range(n)]
for u in uu[::-1]:
    if len(dp[u]) <= k: dp[u].append(1)
    p = parent[u]
    merge(p, u)

print(dp[0][k])
0