問題一覧 > 通常問題

No.196 Typical DP (1)

レベル : / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 256 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 161
作問者 : evima
お気に入りにしたユーザー ProblemId : 234 / 自分の提出
問題文最終更新日: 2026-06-25 06:20:13
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder open 2015 small (順位表) の他の問題:

Problem Statement

You are given a rooted tree with $N$ vertices and an integer $K$. Consider painting each vertex of this tree white or black.
Find the number of ways to paint the vertices that satisfy both of the following two conditions, modulo $1,000,000,007$.
1. Exactly $K$ vertices are painted black.
2. For any vertex $v$ painted black, every child of $v$ is also painted black.

(For terminology, please refer to Wikipedia: Tree (mathematics) - Wikipedia)

Input

$N$ $K$
$a_0$ $b_0$
:
$a_{N-2}$ $b_{N-2}$

$2 \le N \le 2000,$ $0 \le K \le N$
The vertices of the tree are numbered from $0$ to $N-1$, and vertex $0$ is the root of the tree. For each $i$ $(0 \le i \le N-2)$, $a_i, b_i$ $(0 \le a_i \lt b_i \le N-1)$ indicates that the two vertices $a_i, b_i$ are directly connected by an edge. The input graph is guaranteed to be a tree.

Output

Output the number of valid ways to paint the vertices modulo $1,000,000,007$, followed by a newline.

Sample

Sample 1
Input
6 1
0 1
0 2
1 3
2 4
2 5
Output
3

The conditions are satisfied by painting any one of the vertices $3, 4, 5$ black and all the remaining vertices white.
Note that if a vertex $v$ has no children, the condition "every child of $v$ is also painted black" always holds.

Sample 2
Input
6 2
0 1
0 2
1 3
2 4
2 5
Output
4

The conditions are satisfied when the set of vertices painted black is one of $\{1, 3\}, \{3, 4\}, \{3, 5\}, \{4, 5\}$.

Sample 3
Input
6 3
0 1
0 2
1 3
2 4
2 5
Output
4

The conditions are satisfied when the set of vertices painted black is one of $\{1, 3, 4\}, \{1, 3, 5\}, \{2, 4, 5\}, \{3, 4, 5\}$.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。