結果
問題 |
No.196 典型DP (1)
|
ユーザー |
![]() |
提出日時 | 2019-06-15 17:02:45 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 241 ms / 2,000 ms |
コード長 | 1,359 bytes |
コンパイル時間 | 358 ms |
コンパイル使用メモリ | 82,352 KB |
実行使用メモリ | 114,428 KB |
最終ジャッジ日時 | 2024-11-19 03:01:07 |
合計ジャッジ時間 | 6,706 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 41 |
ソースコード
import sys sys.setrecursionlimit(100000) n, k = map(int, input().split()) info = [list(map(int, input().split())) for i in range(n-1)] MOD = 10**9 + 7 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][i] %= MOD dp[pos][cnt] = 1 return cnt visited = [False] * n visited[0] = True dfs(0) print(dp[0][k] % MOD)