結果

問題 No.1075 木の上の山
ユーザー beetbeet
提出日時 2020-06-05 22:16:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,928 ms / 2,000 ms
コード長 3,798 bytes
コンパイル時間 2,680 ms
コンパイル使用メモリ 228,120 KB
実行使用メモリ 27,056 KB
最終ジャッジ日時 2023-08-22 16:01:53
合計ジャッジ時間 11,218 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 4 ms
4,380 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 39 ms
26,816 KB
testcase_18 AC 1,845 ms
26,768 KB
testcase_19 AC 39 ms
26,816 KB
testcase_20 AC 1,928 ms
26,884 KB
testcase_21 AC 46 ms
26,760 KB
testcase_22 AC 42 ms
26,808 KB
testcase_23 AC 43 ms
26,748 KB
testcase_24 AC 45 ms
26,876 KB
testcase_25 AC 42 ms
26,776 KB
testcase_26 AC 992 ms
26,888 KB
testcase_27 AC 505 ms
26,880 KB
testcase_28 AC 45 ms
26,768 KB
testcase_29 AC 453 ms
26,776 KB
testcase_30 AC 668 ms
27,056 KB
testcase_31 AC 452 ms
26,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
using Int = long long;
const char newl = '\n';


template<typename T,T MOD = 1000000007>
struct Mint{
  static constexpr T mod = MOD;
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;}
  Mint operator-(Mint a) const{return Mint(v)-=a;}
  Mint operator*(Mint a) const{return Mint(v)*=a;}
  Mint operator/(Mint a) const{return Mint(v)/=a;}

  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}
  bool operator <(const Mint a)const{return v <a.v;}

  static Mint comb(long long n,int k){
    Mint num(1),dom(1);
    for(int i=0;i<k;i++){
      num*=Mint(n-i);
      dom*=Mint(i+1);
    }
    return num/dom;
  }
};
template<typename T,T MOD> constexpr T Mint<T, MOD>::mod;
template<typename T,T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}


template<typename V>
V compress(V vs){
  sort(vs.begin(),vs.end());
  vs.erase(unique(vs.begin(),vs.end()),vs.end());
  return vs;
}
template<typename T>
map<T, int> dict(const vector<T> &vs){
  map<T, int> res;
  for(int i=0;i<(int)vs.size();i++)
    res[vs[i]]=i;
  return res;
}
map<char, int> dict(const string &s){
  return dict(vector<char>(s.begin(),s.end()));
}


template<typename F>
struct FixPoint : F{
  FixPoint(F&& f):F(forward<F>(f)){}
  template<typename... Args>
  decltype(auto) operator()(Args&&... args) const{
    return F::operator()(*this,forward<Args>(args)...);
  }
};
template<typename F>
inline decltype(auto) MFP(F&& f){
  return FixPoint<F>{forward<F>(f)};
}

//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,k;
  cin>>n>>k;
  vector<vector<int>> G(n);

  using P = pair<int, int>;
  vector<P> vp;
  for(int i=1;i<n;i++){
    int a,b;
    cin>>a>>b;
    a--;b--;
    G[a].emplace_back(b);
    G[b].emplace_back(a);
    vp.emplace_back(a,b);
    vp.emplace_back(b,a);
  }

  for(int i=0;i<n;i++)
    vp.emplace_back(i,-1);
  vp=compress(vp);
  auto dc=[&](auto p)->int{
    return lower_bound(vp.begin(),vp.end(),p)-vp.begin();
  };

  using M = Mint<int>;
  vector<vector<M>> dp(vp.size());
  // [0, i]
  vector<vector<M>> sm(vp.size());

  auto pre=
    MFP([&](auto dfs,int v,int p)->void{
      int idx=dc(P(v,p));
      if(!dp[idx].empty()) return;
      dp[idx].assign(k,M(1));

      for(int u:G[v]){
        if(u==p) continue;
        dfs(u,v);
        int idy=dc(P(u,v));
        for(int i=0;i<k;i++)
          dp[idx][i]*=sm[idy][i];
      }

      sm[idx]=dp[idx];
      for(int i=1;i<k;i++)
        sm[idx][i]+=sm[idx][i-1];
    });

  for(int i=0;i<n;i++) pre(i,-1);

  M ans{0};
  MFP([&](auto dfs,int v,int p)->void{
    for(int u:G[v]){
      if(u==p) continue;
      dfs(u,v);
    }

    int idx=dc(P(v,p));
    if(p<0){
      for(int i=0;i<k;i++)
        ans+=dp[idx][i];
    }else{
      int idy=dc(P(p,v));
      for(int i=1;i<k;i++)
        ans+=dp[idx][i]*sm[idy][i-1];
    }
  })(0,-1);

  cout<<ans<<newl;
  return 0;
}
0