結果

問題 No.196 典型DP (1)
ユーザー koprickykopricky
提出日時 2018-01-31 03:05:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,836 bytes
コンパイル時間 1,788 ms
コンパイル使用メモリ 174,124 KB
実行使用メモリ 35,428 KB
最終ジャッジ日時 2023-08-28 16:54:09
合計ジャッジ時間 10,783 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 1 ms
4,380 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
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 AC 8 ms
19,284 KB
testcase_27 TLE -
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 #

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 2005;

int memo[MAX_N][MAX_N];
int st[MAX_N];
vector<int> G[MAX_N];

void dfs(int u,int p)
{
    st[u] = 1;
    for(int v : G[u]){
        if(v != p){
            dfs(v,u);
            st[u] += st[v];
        }
    }
}

ll add(ll x,ll y)
{
    return (x + y)%MOD;
}

ll sub(ll x,ll y)
{
    return (x+MOD-y)%MOD;
}

ll mul(ll x,ll y)
{
    return x*y%MOD;
}

int rec(int u,int p,int k)
{
    if(memo[u][k] >= 0){
        return memo[u][k];
    }
    if(st[u] == k || k == 0){
        memo[u][k] = 1;
        return 1;
    }
    vvi dp(len(G[u])+1,vi(k+1,0));
    vvi vec(len(G[u]));
    vi l(len(G[u]));
    int id = 0;
    for(int i = 0;i<len(G[u]);i++){
        int v = G[u][i];
        if(v != p){
            l[id] = max(0,k-st[u]+st[v]+1);
            int sz = st[v]+1-l[id];
            vec[id].resize(sz);
            rep(j,sz){
                vec[id][j] = rec(v,u,j+l[id]);
            }
            id++;
        }
    }
    dp[0][0] = 1;
    rep(i,id){
        rep(m,k+1){
            if(dp[i][m]){
                rep(j,min(len(vec[i]),k+1-l[i]-m)){
                    dp[i+1][m+j+l[i]] = add(dp[i+1][m+j+l[i]],mul(dp[i][m],vec[i][j]));
                }
            }
        }
    }
    rep(i,k+1){
        memo[u][i] = dp[id][i];
    }
    // cout << id << " " << k << " " << memo[u][k] << "\n";
    return memo[u][k];
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,K;
    cin >> n >> K;
    vi a(n),b(n);
    rep(i,n-1){
        int a,b;
        cin >> a >> b;
        G[a].pb(b),G[b].pb(a);
    }
    dfs(0,-1);
    rep(i,n){
        rep(j,n+1){
            memo[i][j] = -1;
        }
    }
    cout << rec(0,-1,K) << "\n";
    return 0;
}
0