結果

問題 No.196 典型DP (1)
ユーザー rlangevinrlangevin
提出日時 2023-01-29 16:23:32
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,131 bytes
コンパイル時間 366 ms
コンパイル使用メモリ 86,964 KB
実行使用メモリ 146,704 KB
最終ジャッジ日時 2023-09-11 23:54:54
合計ジャッジ時間 11,421 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 145 ms
121,776 KB
testcase_01 AC 144 ms
121,916 KB
testcase_02 AC 146 ms
121,992 KB
testcase_03 AC 143 ms
121,736 KB
testcase_04 AC 145 ms
121,976 KB
testcase_05 AC 146 ms
121,724 KB
testcase_06 AC 145 ms
121,860 KB
testcase_07 AC 144 ms
121,828 KB
testcase_08 AC 145 ms
121,572 KB
testcase_09 AC 144 ms
121,832 KB
testcase_10 AC 151 ms
121,892 KB
testcase_11 AC 153 ms
121,956 KB
testcase_12 AC 171 ms
141,200 KB
testcase_13 AC 170 ms
140,720 KB
testcase_14 AC 171 ms
140,900 KB
testcase_15 AC 194 ms
141,088 KB
testcase_16 AC 219 ms
140,044 KB
testcase_17 AC 300 ms
134,144 KB
testcase_18 AC 470 ms
142,324 KB
testcase_19 AC 501 ms
142,392 KB
testcase_20 AC 604 ms
142,240 KB
testcase_21 AC 791 ms
142,068 KB
testcase_22 AC 886 ms
142,436 KB
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
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] * 2005 for i in range(2005)] 
dp1 = [[0] * 2005 for i in range(2005)]
for i in range(2005):
    dp1[i][1] = 1
    dp0[i][0] = 1
     
for u in G:
    for v in G2[u]:
        sz[v] += sz[u]
        merge0 = [0] * (sz[v] + 1)
        merge1 = [0] * (sz[v] + 1) 
        for x in range(sz[u] + 1):
            for y in range(x, sz[v] + 1):
                merge1[y] += dp1[v][y - x] * dp1[u][x]
                merge0[y] += dp0[v][y - x] * dp1[u][x]
                merge0[y] += dp0[v][y - x] * dp0[u][x]
                merge1[y] %= mod
                merge0[y] %= mod
                
        for x in range(sz[v] + 1):
            dp0[v][x] = merge0[x]
            dp1[v][x] = merge1[x]
                
print( (dp0[0][K] + dp1[0][K]) % mod )
0