Problem p02728 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define Int int64_t

using namespace std;

struct Combination {
  vector<int64_t> _fact, _rfact, _inv;
  int64_t MOD;

  Combination(size_t sz, int64_t mod)
      : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1), MOD(mod) {
    _fact[0] = _rfact[sz] = _inv[0] = 1;
    for (int i = 1; i <= sz; ++i) {
      _fact[i] = _fact[i - 1] * i % MOD;
    }
    _rfact[sz] = inverse(_fact[sz]);
    for (int i = sz - 1; i >= 0; --i) {
      _rfact[i] = _rfact[i + 1] * (i + 1) % MOD;
    }
    for (int i = 1; i <= sz; ++i) {
      _inv[i] = _rfact[i] * _fact[i - 1] % MOD;
    }
  }

  inline int64_t fact(int k) const { return _fact[k]; }
  inline int64_t rfact(int k) const { return _rfact[k]; }
  inline int64_t inv(int k) const { return _inv[k]; }

  int64_t inverse(int64_t x) const { return pow(x, MOD - 2); }
  int64_t pow(int64_t x, int64_t n) const {
    int64_t ret = 1;
    while (n > 0) {
      if (n & 1) {
        (ret *= x) %= MOD;
      }
      (x *= x) %= MOD;
      n >>= 1;
    }
    return ret;
  }

  int64_t P(int n, int r) const {
    if (r < 0 || n < r) {
      return 0;
    }
    return fact(n) * rfact(n - r) % MOD;
  }
  int64_t C(int n, int r) const {
    if (r < 0 || n < r) {
      return 0;
    }
    return fact(n) * rfact(r) % MOD * rfact(n - r) % MOD;
  }
  int64_t H(int n, int r) const {
    if (n < 0 || r < 0) {
      return 0;
    }
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};

constexpr Int MOD = 1e9 + 7;
Combination cb(200010, MOD);
vector<vector<int>> g(200010);
vector<pair<Int, Int>> d(200010);
vector<Int> ans(200010, 0);

void tapu(int now, int par) {
  Int a = 0, b = 1, cnt = 1;
  for (int u : g[now]) {
    if (u == par) {
      continue;
    }
    tapu(u, now);
    Int n = d[u].first;
    a += n;
    (b *= cb.rfact(n)) %= MOD;
    (cnt *= d[u].second) %= MOD;
  }
  Int x = cb.fact(a) * b % MOD * cnt % MOD;
  d[now] = {a + 1, x};
}

void tapi(int now, int par, Int n_par, Int c_par) {
  Int a = 0, b = 1, cnt = 1;
  for (int u : g[now]) {
    if (u == par) {
      a += n_par;
      (b *= cb.rfact(n_par)) %= MOD;
      (cnt *= c_par) %= MOD;
      continue;
    }
    Int n = d[u].first;
    a += n;
    (b *= cb.rfact(n)) %= MOD;
    (cnt *= d[u].second) %= MOD;
  }
  ans[now] = cb.fact(a) * b % MOD * cnt % MOD;

  for (int u : g[now]) {
    if (u == par) {
      continue;
    }
    Int n = d[u].first;
    Int u_a = a - n;
    Int u_b = b * cb.fact(n) % MOD;
    Int u_c = cnt * cb.inverse(d[u].second) % MOD;
    Int x = cb.fact(u_a) * u_b % MOD * u_c % MOD;
    tapi(u, now, u_a + 1, x);
  }
}

int main() {
  const Int MOD = 1e9 + 7;
  int N;
  cin >> N;
  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }

  tapu(0, -1);
  tapi(0, -1, 0, 1);
  for (int i = 0; i < N; ++i) {
    cout << ans[i] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define Int int64_t

using namespace std;

struct Combination {
  vector<int64_t> _fact, _rfact, _inv;
  int64_t MOD;

  Combination(size_t sz, int64_t mod)
      : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1), MOD(mod) {
    _fact[0] = _rfact[sz] = _inv[0] = 1;
    for (int i = 1; i <= sz; ++i) {
      _fact[i] = _fact[i - 1] * i % MOD;
    }
    _rfact[sz] = inverse(_fact[sz]);
    for (int i = sz - 1; i >= 0; --i) {
      _rfact[i] = _rfact[i + 1] * (i + 1) % MOD;
    }
    for (int i = 1; i <= sz; ++i) {
      _inv[i] = _rfact[i] * _fact[i - 1] % MOD;
    }
  }

  inline int64_t fact(int k) const { return _fact[k]; }
  inline int64_t rfact(int k) const { return _rfact[k]; }
  inline int64_t inv(int k) const { return _inv[k]; }

  int64_t inverse(int64_t x) const { return pow(x, MOD - 2); }
  int64_t pow(int64_t x, int64_t n) const {
    int64_t ret = 1;
    while (n > 0) {
      if (n & 1) {
        (ret *= x) %= MOD;
      }
      (x *= x) %= MOD;
      n >>= 1;
    }
    return ret;
  }

  int64_t P(int n, int r) const {
    if (r < 0 || n < r) {
      return 0;
    }
    return fact(n) * rfact(n - r) % MOD;
  }
  int64_t C(int n, int r) const {
    if (r < 0 || n < r) {
      return 0;
    }
    return fact(n) * rfact(r) % MOD * rfact(n - r) % MOD;
  }
  int64_t H(int n, int r) const {
    if (n < 0 || r < 0) {
      return 0;
    }
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};

constexpr Int MOD = 1e9 + 7;
Combination cb(200010, MOD);
vector<vector<int>> g(200010);
vector<pair<Int, Int>> d(200010);
vector<Int> ans(200010, 0);

void tapu(int now, int par) {
  Int a = 0, b = 1, cnt = 1;
  for (int u : g[now]) {
    if (u == par) {
      continue;
    }
    tapu(u, now);
    Int n = d[u].first;
    a += n;
    (b *= cb.rfact(n)) %= MOD;
    (cnt *= d[u].second) %= MOD;
  }
  Int x = cb.fact(a) * b % MOD * cnt % MOD;
  d[now] = {a + 1, x};
}

void tapi(int now, int par, Int n_par, Int c_par) {
  Int a = 0, b = 1, cnt = 1;
  for (int u : g[now]) {
    if (u == par) {
      a += n_par;
      (b *= cb.rfact(n_par)) %= MOD;
      (cnt *= c_par) %= MOD;
      continue;
    }
    Int n = d[u].first;
    a += n;
    (b *= cb.rfact(n)) %= MOD;
    (cnt *= d[u].second) %= MOD;
  }
  ans[now] = cb.fact(a) * b % MOD * cnt % MOD;

  for (int u : g[now]) {
    if (u == par) {
      continue;
    }
    a = 0, b = 1, cnt = 1;
    for (int v : g[now]) {
      if (v == u) {
        continue;
      }
      if (v == par) {
        a += n_par;
        (b *= cb.rfact(n_par)) %= MOD;
        (cnt *= c_par) %= MOD;
        continue;
      }
      Int n = d[v].first;
      a += n;
      (b *= cb.rfact(n)) %= MOD;
      (cnt *= d[v].second) %= MOD;
    }
    Int x = cb.fact(a) * b % MOD * cnt % MOD;
    tapi(u, now, a + 1, x);
  }
}

int main() {
  const Int MOD = 1e9 + 7;
  int N;
  cin >> N;
  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }

  tapu(0, -1);
  tapi(0, -1, 0, 1);
  for (int i = 0; i < N; ++i) {
    cout << ans[i] << endl;
  }

  return 0;
}