結果

問題 No.196 典型DP (1)
ユーザー rlangevinrlangevin
提出日時 2023-01-29 16:57:33
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,261 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 82,272 KB
実行使用メモリ 118,832 KB
最終ジャッジ日時 2024-06-29 13:29:52
合計ジャッジ時間 6,555 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 42 ms
54,400 KB
testcase_02 AC 41 ms
54,528 KB
testcase_03 AC 44 ms
54,272 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 42 ms
54,536 KB
testcase_07 WA -
testcase_08 RE -
testcase_09 WA -
testcase_10 WA -
testcase_11 RE -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 TLE -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline = sys.stdin.readline
from collections import deque


def bfs(G, s, N):
    Q = deque([])
    dist = [-1] * N
    par = [-1] * N
    sz = [1] * N
    dist[s] = 0
    for u in G[s]:
        par[u] = s
        dist[u] = 1
        sz[u] += sz[s]
        Q.append(u)

    while Q:
        u = Q.popleft()
        for v in G[u]:
            if dist[v] != -1:
                continue
            dist[v] = dist[u] + 1
            par[v] = u
            sz[v] += sz[u]
            Q.append(v)
            
    return par

def topsort(G):
    Q = deque()
    N = len(G)
    count = [0] * N

    for u in range(N):
        for v in G[u]:
            count[v] += 1

    for u in range(N):
        if count[u] == 0:
            Q.append(u)
    anslst = []
    while Q:
        u = Q.pop()
        anslst.append(u)
        for v in G[u]:
            count[v] -= 1
            if count[v] == 0:
                Q.append(v)
    return anslst


N, K = map(int, input().split())
mod = 10 ** 9 + 7
G = [[] for i in range(N)]
a, b = [0] * (N - 1), [0] * (N - 1)
for i in range(N - 1):
    a[i], b[i] = map(int, input().split())
    G[a[i]].append(b[i])
    G[b[i]].append(a[i])

par = bfs(G, 0, N)
G2 = [[] for i in range(N)]
for i in range(N - 1):
    if par[a[i]] == b[i]:
        G2[a[i]].append(b[i])
    else:
        G2[b[i]].append(a[i])
G = topsort(G2)
sz = [1] * N

dp0 = [[0] * (K + 1) for i in range(N)] 
dp1 = [[0] * (K + 1) for i in range(N)]
for i in range(N):
    dp1[i][1] = 1
    dp0[i][0] = 1
     
for u in G:
    for v in G2[u]:
        pre = sz[v]
        sz[v] += sz[u]
        sz[v] = min(K, sz[v])
        merge0 = [0] * (sz[v] + 1)
        # merge1 = [0] * (sz[v] + 1)
        dp0u = dp0[u]
        dp0v = dp0[v]
        dp1u = dp1[u]
        for x in range(sz[u] + 1):
            d = dp1u[x] + dp0u[x]
            for y in range(x, sz[v] + 1):
                # merge1[y] += dp1v[y - x] * dp1u[x]
                merge0[y] += dp0v[y - x] * (d)
                # merge1[y] %= mod
                merge0[y] %= mod
                
        for x in range(sz[v] + 1):
            dp0[v][x] = merge0[x]
            # dp1[v][x] = merge1[x]
        dp1[v][sz[v]] = 1     
        dp1[v][pre] = 0     
print( (dp0[0][K] + dp1[0][K]) % mod )
0