結果

問題 No.1075 木の上の山
ユーザー nmnmnmnmnmnmnmnmnmnmnmnmnmnm
提出日時 2020-05-16 03:08:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 175 ms / 2,000 ms
コード長 2,314 bytes
コンパイル時間 2,209 ms
コンパイル使用メモリ 97,360 KB
実行使用メモリ 27,704 KB
最終ジャッジ日時 2023-10-19 23:06:36
合計ジャッジ時間 5,378 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
27,536 KB
testcase_01 AC 9 ms
27,540 KB
testcase_02 AC 10 ms
27,540 KB
testcase_03 AC 11 ms
27,540 KB
testcase_04 AC 11 ms
27,540 KB
testcase_05 AC 12 ms
27,540 KB
testcase_06 AC 13 ms
27,540 KB
testcase_07 AC 25 ms
27,548 KB
testcase_08 AC 25 ms
27,548 KB
testcase_09 AC 27 ms
27,548 KB
testcase_10 AC 29 ms
27,548 KB
testcase_11 AC 30 ms
27,548 KB
testcase_12 AC 32 ms
27,552 KB
testcase_13 AC 33 ms
27,552 KB
testcase_14 AC 35 ms
27,552 KB
testcase_15 AC 37 ms
27,552 KB
testcase_16 AC 38 ms
27,552 KB
testcase_17 AC 169 ms
27,704 KB
testcase_18 AC 174 ms
27,624 KB
testcase_19 AC 171 ms
27,688 KB
testcase_20 AC 175 ms
27,624 KB
testcase_21 AC 169 ms
27,620 KB
testcase_22 AC 169 ms
27,620 KB
testcase_23 AC 168 ms
27,620 KB
testcase_24 AC 169 ms
27,620 KB
testcase_25 AC 170 ms
27,620 KB
testcase_26 AC 174 ms
27,628 KB
testcase_27 AC 171 ms
27,624 KB
testcase_28 AC 170 ms
27,652 KB
testcase_29 AC 172 ms
27,628 KB
testcase_30 AC 173 ms
27,632 KB
testcase_31 AC 173 ms
27,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <assert.h>

using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define per(i,a,b) for(ll i=b-1LL;i>=(a);--i)
#define clr(a, b) memset((a), (b) ,sizeof(a))
#define ctos(c) string(1,c)
#define endl "\n"
#define print(x) cout<<#x<<" = "<<x<<endl;

#define MOD 1000000007

ll dp1[1010][1010];
ll dp2[1010][1010];
ll dp3[1010][1010];

long long modpow(long long a, long long b){ 
  long long r = 1LL;
  while(b){
    if(b & 1LL)r *= a;
    if(r >= MOD)r %= MOD;
    a *= a;
    if(a >= MOD)a %= MOD;
    b >>= 1LL;
  }
  return r%MOD;
}

vector<vector<ll> > vv;

void dfs1(ll a, ll p){
  rep(i,1,1005){
    ll b = 1;
    rep(j,0,vv[a].sz){
      if(vv[a][j]==p)continue;
      if(i==1)dfs1(vv[a][j],a);
      b *= dp1[vv[a][j]][i];
      b %= MOD;
    }
    dp1[a][i] = dp1[a][i-1]+b;
    dp1[a][i] %= MOD;
  }
}

void dfs2(ll a, ll p){
  ll e = 0;
  rep(i,1,1005){
    if(p==-1){
      dp2[a][i] = dp1[a][i];
      dp3[a][i] = MOD+dp1[a][i]-dp1[a][i-1];
      dp3[a][i] %= MOD;
    }
    else{
      dp3[a][i] = e*(MOD+dp1[a][i]-dp1[a][i-1]);
      dp3[a][i] %= MOD;
      ll b = (MOD+dp2[p][i]-dp2[p][i-1])%MOD;
      b *= modpow(dp1[a][i],MOD-2);
      b %= MOD;
      b += e;
      b %= MOD;
      e = b;
      dp2[a][i] = b*(MOD+dp1[a][i]-dp1[a][i-1]);
      dp2[a][i] += dp2[a][i-1];
      dp2[a][i] %= MOD;
    }
  }
  rep(j,0,vv[a].sz){
    if(vv[a][j]==p)continue;
    dfs2(vv[a][j],a);
  }
}

int main(){
  ll n,k;
  cin>>n>>k;
  vector<vector<ll> > vv_(n,vector<ll>());
  vv = vv_; 
  rep(i,0,n-1){
    ll a,b;
    cin>>a>>b;
    a--;b--;
    vv[a].pb(b);
    vv[b].pb(a);
  }
  clr(dp1,0);
  clr(dp2,0);
  clr(dp3,0);
  dfs1(0,-1);
  dfs2(0,-1);
  ll ans = 0;
  rep(i,0,n){
    rep(j,1,k+1){
      ans += dp3[i][j];
      ans %= MOD;
    }
  }
  cout << ans << endl;
  return 0;
}
0