結果

問題 No.196 典型DP (1)
ユーザー hashiryohashiryo
提出日時 2020-05-17 12:19:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,010 bytes
コンパイル時間 1,997 ms
コンパイル使用メモリ 184,208 KB
実行使用メモリ 20,352 KB
最終ジャッジ日時 2024-09-25 03:28:19
合計ジャッジ時間 3,721 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 WA -
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 WA -
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 3 ms
6,944 KB
testcase_16 AC 3 ms
6,944 KB
testcase_17 AC 4 ms
6,940 KB
testcase_18 AC 6 ms
6,940 KB
testcase_19 AC 7 ms
6,940 KB
testcase_20 AC 9 ms
6,944 KB
testcase_21 AC 8 ms
6,940 KB
testcase_22 AC 8 ms
6,940 KB
testcase_23 AC 16 ms
10,240 KB
testcase_24 AC 14 ms
8,320 KB
testcase_25 AC 13 ms
7,808 KB
testcase_26 AC 25 ms
20,224 KB
testcase_27 AC 26 ms
20,200 KB
testcase_28 AC 25 ms
20,224 KB
testcase_29 AC 25 ms
20,352 KB
testcase_30 WA -
testcase_31 AC 16 ms
6,940 KB
testcase_32 AC 16 ms
6,940 KB
testcase_33 AC 16 ms
6,944 KB
testcase_34 AC 16 ms
6,944 KB
testcase_35 WA -
testcase_36 AC 15 ms
6,940 KB
testcase_37 AC 9 ms
6,940 KB
testcase_38 AC 15 ms
6,940 KB
testcase_39 AC 13 ms
6,940 KB
testcase_40 WA -
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 1 ms
6,940 KB
testcase_43 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                           \
  for (long long hoge = 0; (hoge) < (n); ++(hoge)) \
  cerr << #x << "[" << hoge << "]: " << x[hoge] << endl

template <typename T, typename E>
struct Tree_DP {
  struct Edge {
    int to;
    E data;
    T dp, ndp;
  };
  using F = function<T(T, T)>;
  using G = function<T(T, E)>;

 private:
  vector<vector<Edge>> graph;
  vector<T> subdp, dp;
  const T ti;
  const F f;
  const G g;

 private:
  void dfs_sub(int idx, int par) {
    for (auto &e : graph[idx]) {
      if (e.to == par) continue;
      dfs_sub(e.to, idx);
      subdp[idx] = f(subdp[idx], g(subdp[e.to], e.data));
    }
  }

  void dfs_all(int idx, int par, const T &top) {
    T buff{ti};
    for (int i = 0; i < (int)graph[idx].size(); i++) {
      auto &e = graph[idx][i];
      e.ndp = buff;
      e.dp = g(par == e.to ? top : subdp[e.to], e.data);
      buff = f(buff, e.dp);
    }
    dp[idx] = buff;
    buff = ti;
    for (int i = (int)graph[idx].size() - 1; i >= 0; i--) {
      auto &e = graph[idx][i];
      if (e.to != par) dfs_all(e.to, idx, f(e.ndp, buff));
      e.ndp = f(e.ndp, buff);
      buff = f(buff, e.dp);
    }
  }

 public:
  Tree_DP(int V, const F f, const G g, T ti)
      : graph(V), f(f), g(g), ti(ti), subdp(V, ti), dp(V, ti) {}

  void add_edge(int u, int v, E d, E e) {
    graph[u].emplace_back((Edge){v, d, ti, ti});
    graph[v].emplace_back((Edge){u, e, ti, ti});
  }
  void add_edge(int u, int v, E d) { add_edge(u, v, d, d); }

  vector<T> build(int root) {
    dfs_sub(root, -1);
    return subdp;
  }

  vector<T> build_rerooting() {
    dfs_sub(0, -1);
    dfs_all(0, -1, ti);
    return dp;
  }
};

template <int mod>
struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod)) {}
  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) { return *this *= p.inverse(); }
  ModInt operator-() const { return ModInt() - *this; }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b) t = a / b, swap(a -= t * b, b), swap(u -= t * v, v);
    return ModInt(u);
  }
  ModInt pow(int64_t e) const {
    ModInt ret(1);
    for (ModInt b = *this; e; e >>= 1, b *= b)
      if (e & 1) ret *= b;
    return ret;
  }
  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  static int modulo() { return mod; }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  using Mint = ModInt<int(1e9 + 7)>;
  int N, K;
  cin >> N >> K;
  auto f = [](const vector<Mint> &v, const vector<Mint> &ch) {
    vector<Mint> ret(v.size() + ch.size() - 1, 0);
    for (int i = 0; i < v.size(); i++) {
      for (int j = 0; j < ch.size(); j++) {
        ret[i + j] += v[i] * ch[j];
      }
    }
    return ret;
  };
  auto g = [](const vector<Mint> &v, int dat) {
    auto ret = v;
    ret.emplace_back(1);
    return ret;
  };
  Tree_DP<vector<Mint>, int> G(N, f, g, {1});
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    G.add_edge(a, b, 0);
  }
  auto ans = G.build(0);
  cout << ans[0][K] << endl;
  return 0;
}
0