結果

問題 No.196 典型DP (1)
ユーザー tsutaj
提出日時 2018-06-09 12:43:14
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,012 bytes
コンパイル時間 454 ms
コンパイル使用メモリ 54,348 KB
実行使用メモリ 34,200 KB
最終ジャッジ日時 2024-06-30 12:30:30
合計ジャッジ時間 2,190 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 14 WA * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
using Graph = vector< vector<int> >;

const long long int MOD = 1000000007LL;
int N, K, num[2010];
long long int dp[2010][2010];

void dfs(Graph &G, int cur) {
    num[cur] = 1;
    for(auto to : G[cur]) {
        dfs(G, to);
        num[cur] += num[to];
    }
}

void solve(Graph &G, int cur) {
    // 全く塗らない、全部塗る
    dp[cur][0] = 1;
    int sum = 0;
    for(auto to : G[cur]) {
        // cur は塗らない
        solve(G, to);
        for(int k=sum; k>=0; k--) {
            for(int pt=num[to]; pt>=1; pt--) {
                (dp[cur][k+pt] += dp[cur][k] * dp[to][pt]) %= MOD;
            }
        }
        sum += num[to];
    }
    dp[cur][ num[cur] ] = 1;
}

int main() {
    scanf("%d%d", &N, &K);

    Graph G(N);
    for(int i=0; i<N-1; i++) {
        int u, v; scanf("%d%d", &u, &v);
        G[u].push_back(v);
    }

    dfs(G, 0);
    solve(G, 0);

    printf("%lld\n", dp[0][K]);
    return 0;
}
0