結果

問題 No.196 典型DP (1)
ユーザー tentententen
提出日時 2021-05-20 10:20:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 312 ms / 2,000 ms
コード長 1,994 bytes
コンパイル時間 2,255 ms
コンパイル使用メモリ 78,292 KB
実行使用メモリ 57,968 KB
最終ジャッジ日時 2024-04-18 14:00:05
合計ジャッジ時間 11,001 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
37,048 KB
testcase_01 AC 55 ms
37,200 KB
testcase_02 AC 50 ms
36,908 KB
testcase_03 AC 53 ms
37,252 KB
testcase_04 AC 52 ms
36,796 KB
testcase_05 AC 53 ms
37,116 KB
testcase_06 AC 53 ms
37,340 KB
testcase_07 AC 51 ms
36,980 KB
testcase_08 AC 54 ms
37,116 KB
testcase_09 AC 52 ms
36,932 KB
testcase_10 AC 54 ms
37,172 KB
testcase_11 AC 53 ms
37,156 KB
testcase_12 AC 51 ms
37,204 KB
testcase_13 AC 55 ms
37,192 KB
testcase_14 AC 53 ms
37,312 KB
testcase_15 AC 82 ms
38,772 KB
testcase_16 AC 122 ms
42,480 KB
testcase_17 AC 189 ms
48,240 KB
testcase_18 AC 227 ms
52,468 KB
testcase_19 AC 268 ms
56,704 KB
testcase_20 AC 214 ms
57,616 KB
testcase_21 AC 219 ms
57,596 KB
testcase_22 AC 226 ms
57,264 KB
testcase_23 AC 255 ms
57,968 KB
testcase_24 AC 248 ms
57,648 KB
testcase_25 AC 212 ms
57,756 KB
testcase_26 AC 283 ms
57,332 KB
testcase_27 AC 287 ms
57,428 KB
testcase_28 AC 302 ms
57,492 KB
testcase_29 AC 277 ms
57,596 KB
testcase_30 AC 312 ms
57,544 KB
testcase_31 AC 190 ms
57,480 KB
testcase_32 AC 165 ms
55,680 KB
testcase_33 AC 187 ms
57,484 KB
testcase_34 AC 196 ms
57,104 KB
testcase_35 AC 185 ms
57,324 KB
testcase_36 AC 240 ms
57,656 KB
testcase_37 AC 268 ms
57,804 KB
testcase_38 AC 259 ms
57,600 KB
testcase_39 AC 299 ms
57,716 KB
testcase_40 AC 234 ms
57,896 KB
testcase_41 AC 53 ms
37,248 KB
testcase_42 AC 54 ms
36,928 KB
testcase_43 AC 54 ms
37,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
    }
}
0