結果

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

ソースコード

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, int par=-1) {
    num[cur] = 1;
    for(auto to : G[cur]) {
        if(to == par) continue;
        dfs(G, to, cur);
        num[cur] += num[to];
    }
}

void solve(Graph &G, int cur, int par=-1) {
    // 全く塗らない、全部塗る
    dp[cur][0] = dp[cur][ num[cur] ] = 1;
    int sum = 0;
    for(auto to : G[cur]) {
        if(to == par) continue;
        // cur は塗らない
        solve(G, to, cur);
        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];
    }
}

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);
        G[v].push_back(u);
    }

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

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