結果

問題 No.196 典型DP (1)
ユーザー neterukunneterukun
提出日時 2019-06-15 17:00:22
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,260 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 82,528 KB
実行使用メモリ 179,060 KB
最終ジャッジ日時 2024-11-19 02:57:58
合計ジャッジ時間 12,146 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18 WA * 18 RE * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

n, k = map(int, input().split())
info = [list(map(int, input().split())) for i in range(n-1)]

tree = [[] for i in range(n)]
for i in range(n-1):
    tree[info[i][0]].append(info[i][1])
    tree[info[i][1]].append(info[i][0])

# dp[pos][j] := 頂点posを根とする部分木から、
# ちょうどj個の頂点を条件2に従って黒で塗る方法の数
dp = [[0]*(n+1) for i in range(n)]

#0個の頂点を黒で塗る通り数は、それぞれの部分木に対して1通り
for i in range(n):
    dp[i][0] = 1

def dfs(pos):
    cnt = 1
    #部分木が葉のとき
    if all([visited[i] for i in tree[pos]]):
        dp[pos][cnt] = 1
        return cnt #cnt = 1

    #部分木が葉でないとき
    for child_pos in tree[pos]:
        if not visited[child_pos]:
            visited[child_pos] = True
            cnt_child = dfs(child_pos)
            tmp = [0]*(cnt + cnt_child + 1)
            for i in range(cnt+1):
                for j in range(cnt_child+1):
                    tmp[i+j] += dp[pos][i] * dp[child_pos][j]
            cnt += cnt_child
            for i in range(cnt+1):
                dp[pos][i] = tmp[i]

    dp[pos][cnt] = 1 
    return cnt

visited = [False] * n
visited[0] = True
dfs(0)
print(dp[0][k])
      
        
0