結果

問題 No.1075 木の上の山
ユーザー chocoruskchocorusk
提出日時 2020-06-05 22:33:50
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 128 ms / 2,000 ms
コード長 2,300 bytes
コンパイル時間 1,446 ms
コンパイル使用メモリ 132,920 KB
実行使用メモリ 27,368 KB
最終ジャッジ日時 2023-08-22 16:23:01
合計ジャッジ時間 4,293 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,436 KB
testcase_01 AC 3 ms
7,748 KB
testcase_02 AC 2 ms
7,776 KB
testcase_03 AC 2 ms
7,500 KB
testcase_04 AC 3 ms
7,516 KB
testcase_05 AC 2 ms
7,624 KB
testcase_06 AC 3 ms
7,584 KB
testcase_07 AC 5 ms
10,460 KB
testcase_08 AC 6 ms
10,512 KB
testcase_09 AC 5 ms
10,636 KB
testcase_10 AC 6 ms
10,712 KB
testcase_11 AC 6 ms
10,732 KB
testcase_12 AC 6 ms
10,904 KB
testcase_13 AC 6 ms
11,092 KB
testcase_14 AC 7 ms
11,056 KB
testcase_15 AC 7 ms
11,292 KB
testcase_16 AC 7 ms
11,276 KB
testcase_17 AC 90 ms
27,368 KB
testcase_18 AC 117 ms
27,256 KB
testcase_19 AC 94 ms
27,360 KB
testcase_20 AC 128 ms
27,156 KB
testcase_21 AC 99 ms
27,184 KB
testcase_22 AC 96 ms
27,208 KB
testcase_23 AC 98 ms
27,188 KB
testcase_24 AC 99 ms
27,312 KB
testcase_25 AC 98 ms
27,232 KB
testcase_26 AC 120 ms
27,224 KB
testcase_27 AC 109 ms
27,188 KB
testcase_28 AC 97 ms
27,200 KB
testcase_29 AC 112 ms
27,200 KB
testcase_30 AC 120 ms
27,168 KB
testcase_31 AC 112 ms
27,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD=1e9+7;
int n, k;
ll dp[1010][1010];
ll s[1010][1010];
vector<int> g[1010];
void dfs(int x, int p){
    for(auto y:g[x]){
        if(y==p) continue;
        dfs(y, x);
    }
    for(int i=1; i<=k; i++){
        dp[x][i]=1;
        for(auto y:g[x]){
            if(y==p) continue;
            (dp[x][i]*=s[y][i])%=MOD;
        }
    }
    for(int i=1; i<=k; i++){
        s[x][i]=(s[x][i-1]+dp[x][i])%MOD;
    }
}
ll dp2[1010][1010];
void dfs2(int x, int p){
    for(int i=1; i<=k; i++){
        vector<ll> pl(1+g[x].size()), pr(1+g[x].size());
        pl[0]=1;
        for(int j=0; j<g[x].size(); j++){
            int y=g[x][j];
            if(y==p){
                pl[j+1]=pl[j]*dp2[x][i]%MOD;
            }else{
                pl[j+1]=pl[j]*s[y][i]%MOD;
            }
        }
        pr[g[x].size()]=1;
        for(int j=(int)g[x].size()-1; j>=0; j--){
            int y=g[x][j];
            if(y==p){
                pr[j]=pr[j+1]*dp2[x][i]%MOD;
            }else{
                pr[j]=pr[j+1]*s[y][i]%MOD;
            }
        }
        for(int j=0; j<g[x].size(); j++){
            int y=g[x][j];
            if(y==p) continue;
            dp2[y][i]=(dp2[y][i-1]+pl[j]*pr[j+1])%MOD;
        }
    }
    for(auto y:g[x]){
        if(y==p) continue;
        dfs2(y, x);
    }
}
int main()
{
    cin>>n>>k;
    for(int i=0; i<n-1; i++){
        int a, b; cin>>a>>b;a--; b--;
        g[a].push_back(b); g[b].push_back(a);
    }
    dfs(0, -1);
    for(int i=1; i<=k; i++) dp2[0][i]=1;
    dfs2(0, -1);
    ll ans=0;
    for(int i=0; i<n; i++){
        for(int j=1; j<=k; j++){
            if(i==0) (ans+=dp[i][j])%=MOD;
            else (ans+=dp[i][j]*dp2[i][j-1])%=MOD;
        }
    }
    cout<<ans<<endl;
    return 0;
}
0