結果

問題 No.196 典型DP (1)
ユーザー nebukuro09nebukuro09
提出日時 2017-05-12 11:27:17
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 103 ms / 2,000 ms
コード長 1,674 bytes
コンパイル時間 642 ms
コンパイル使用メモリ 101,820 KB
実行使用メモリ 37,504 KB
最終ジャッジ日時 2023-09-03 13:21:13
合計ジャッジ時間 3,694 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 3 ms
4,880 KB
testcase_16 AC 9 ms
8,300 KB
testcase_17 AC 17 ms
16,684 KB
testcase_18 AC 27 ms
21,444 KB
testcase_19 AC 34 ms
29,352 KB
testcase_20 AC 44 ms
35,564 KB
testcase_21 AC 45 ms
36,268 KB
testcase_22 AC 45 ms
36,284 KB
testcase_23 AC 51 ms
36,244 KB
testcase_24 AC 51 ms
36,200 KB
testcase_25 AC 49 ms
35,948 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 53 ms
37,504 KB
testcase_28 AC 52 ms
36,312 KB
testcase_29 AC 52 ms
36,320 KB
testcase_30 AC 52 ms
36,504 KB
testcase_31 AC 3 ms
4,376 KB
testcase_32 AC 103 ms
36,728 KB
testcase_33 AC 103 ms
35,748 KB
testcase_34 AC 103 ms
35,568 KB
testcase_35 AC 103 ms
36,472 KB
testcase_36 AC 3 ms
4,376 KB
testcase_37 AC 48 ms
35,536 KB
testcase_38 AC 97 ms
35,744 KB
testcase_39 AC 87 ms
35,524 KB
testcase_40 AC 101 ms
36,508 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;


void main() {
    immutable long MOD = 10^^9 + 7;
    
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1];
    auto edges = new int[][](N);
    foreach (i; 0..N-1) {
        s = readln.split.map!(to!int);
        edges[s[0]] ~= s[1];
        edges[s[1]] ~= s[0];
    }

    if (K == 0) {
        writeln(1);
        return;
    }


    auto children = new int[](N);
    auto dp = new long[][](N, N+2);
    foreach (i; 0..N) fill(dp[i], 0);
    auto tmp = new long[](N+1);

    int dfs1(int n, int prev) {
        children[n] = 1;
        foreach (m; edges[n]) if (m != prev) children[n] += dfs1(m, n);
        return children[n];
    }
    
    void dfs2(int n, int prev) {
        foreach (m; edges[n]) if (m != prev) dfs2(m, n);

        dp[n][children[n]] = 1;
        if (children[n] == 1) return;
        
        foreach (m; edges[n]) {
            if (m == prev) continue;
            fill(tmp, 0);
            foreach (i; 0..children[n]) {
                if (dp[n][i] == 0) continue;
                foreach (j; 0..children[m]+1) {
                    if (i + j > children[n]) break;
                    tmp[i+j] += (dp[n][i] * dp[m][j]) % MOD;
                    tmp[i+j] %= MOD;
                }
            }

            foreach (i; 0..children[n]+1) (dp[n][i] += tmp[i]) %= MOD;
            foreach (i; 0..children[m]+1) (dp[n][i] += dp[m][i]) %= MOD;
        }

    }

    dfs1(0, -1);
    dfs2(0, -1);
    //dp.each!writeln;
    dp[0][K].writeln;
}
0