結果
| 問題 |
No.196 典型DP (1)
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2021-05-20 10:20:10 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 299 ms / 2,000 ms |
| コード長 | 1,994 bytes |
| コンパイル時間 | 2,223 ms |
| コンパイル使用メモリ | 78,532 KB |
| 実行使用メモリ | 67,660 KB |
| 最終ジャッジ日時 | 2024-10-10 07:12:00 |
| 合計ジャッジ時間 | 10,961 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 41 |
ソースコード
import java.io.*;
import java.util.*;
public class Main {
static int[][] dp;
static int[] children;
static final int MOD = 1000000007;
static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
int k = sc.nextInt();
for (int i = 0; i < n; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < n - 1; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
graph.get(a).add(b);
graph.get(b).add(a);
}
dp = new int[n][n + 1];
children = new int[n];
getChildren(0, 0);
System.out.println(dp[0][k]);
}
static int getChildren(int idx, int parent) {
int count = 1;
dp[idx][0] = 1;
for (int x : graph.get(idx)) {
if (x == parent) {
continue;
}
int tmp = getChildren(x, idx);
for (int i = count - 1; i >= 0; i--) {
for (int j = 1; j <= children[x]; j++) {
dp[idx][i + j] += (int)((long)dp[idx][i] * dp[x][j] % MOD);
dp[idx][i + j] %= MOD;
}
}
count += tmp;
}
dp[idx][count] = 1;
return children[idx] = count;
}
}
class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public Scanner() throws Exception {
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public long nextLong() throws Exception {
return Long.parseLong(next());
}
public String next() throws Exception {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten