結果

問題 No.196 典型DP (1)
ユーザー koprickykopricky
提出日時 2018-01-31 01:43:07
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 3,087 bytes
コンパイル時間 1,414 ms
コンパイル使用メモリ 153,208 KB
実行使用メモリ 35,252 KB
最終ジャッジ日時 2023-08-28 16:47:49
合計ジャッジ時間 18,593 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
35,252 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 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,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 8 ms
7,140 KB
testcase_16 AC 38 ms
9,624 KB
testcase_17 AC 191 ms
14,176 KB
testcase_18 AC 567 ms
16,432 KB
testcase_19 AC 666 ms
18,220 KB
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 AC 1,176 ms
20,952 KB
testcase_25 AC 914 ms
21,680 KB
testcase_26 AC 10 ms
19,320 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])),val(len(G[u]));
    int id = 0;
    int rem = 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];
            val[id] = st[v];
            rem += st[v];
            vec[id].resize(sz);
            rep(j,sz){
                vec[id][j] = rec(v,u,j+l[id]);
            }
            id++;
        }
    }
    dp[0][0] = 1;
    int mx = 0;
    rep(i,id){
        int cri = mx;
        srep(m,max(0,k-rem),min(mx+1,k+1)){
            if(dp[i][m]){
                srep(j,max(0,k-m-l[i]-rem),min(len(vec[i]),k-l[i]-m+1)){
                    dp[i+1][m+j+l[i]] = add(dp[i+1][m+j+l[i]],mul(dp[i][m],vec[i][j]));
                }
                cmx(cri,m+min(len(vec[i]),k-l[i]-m+1)+l[i]-1);
            }
        }
        rem -= val[i];
        cmx(mx,cri);
    }
    memo[u][k] = dp[id][k];
    // 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