結果

問題 No.196 典型DP (1)
ユーザー nebukuro09nebukuro09
提出日時 2017-05-12 11:12:25
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,523 bytes
コンパイル時間 634 ms
コンパイル使用メモリ 101,916 KB
実行使用メモリ 47,508 KB
最終ジャッジ日時 2023-09-03 13:20:18
合計ジャッジ時間 11,081 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 WA -
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 TLE -
testcase_24 WA -
testcase_25 WA -
testcase_26 TLE -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

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];
    }


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

    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;
            auto tmp = new long[](children[n]+1);
            fill(tmp, 0);
            foreach (i; 0..children[n]) {
                foreach (j; 0..children[m]+1) {
                    if (i + j > children[n]) break;
                    tmp[i+j] += dp[n][i] * dp[m][j];
                }
            }

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

    }

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