結果

問題 No.196 典型DP (1)
ユーザー neterukunneterukun
提出日時 2019-06-15 17:00:22
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,260 bytes
コンパイル時間 155 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 178,968 KB
最終ジャッジ日時 2024-04-29 19:57:44
合計ジャッジ時間 11,770 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
51,968 KB
testcase_01 AC 37 ms
52,096 KB
testcase_02 AC 37 ms
51,840 KB
testcase_03 AC 36 ms
51,840 KB
testcase_04 AC 37 ms
51,840 KB
testcase_05 AC 36 ms
52,096 KB
testcase_06 AC 36 ms
52,096 KB
testcase_07 AC 38 ms
51,840 KB
testcase_08 AC 38 ms
52,224 KB
testcase_09 AC 38 ms
52,224 KB
testcase_10 AC 37 ms
52,352 KB
testcase_11 AC 42 ms
57,728 KB
testcase_12 WA -
testcase_13 AC 46 ms
59,648 KB
testcase_14 AC 47 ms
60,288 KB
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 WA -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 AC 455 ms
126,484 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 459 ms
126,536 KB
testcase_36 AC 496 ms
135,936 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 467 ms
133,632 KB
testcase_41 AC 39 ms
51,968 KB
testcase_42 AC 39 ms
52,352 KB
testcase_43 AC 39 ms
51,840 KB
権限があれば一括ダウンロードができます

ソースコード

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