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

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep2(i, m, n) for (int i = int(m); i < int(n); i++)
#define rep(i, n) rep2(i, 0, n)
#define all(a) a.begin(), a.end()
using ll = long long;
using ld = long double;
using V = vector<int>;
using Vll = vector<ll>;
using Vld = vector<ld>;
using VV = vector<V>;
using VVll = vector<Vll>;
using VVld = vector<Vld>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using Pld = pair<ld, ld>;
const int INF = 1 << 30;
const ll INFll = 1ll << 62;
const ld EPS = 1e-10;
const int MOD = int(1e9) + 7;
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline int Log2(T &x) {
  int k;
  for (k = 0; (T(1) << k) <= x; ++k)
    ;
  return k;
} // number of binary digits

const int mod = 1000000007;
struct mint {
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
using Vm = vector<mint>;

// initialization
const int MAX = int(2e5) + 10;
long long fac[MAX], finv[MAX], inv[MAX];
void binom_init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// binom mod MOD
long long binom(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

using Graph = vector<vector<int>>;
Graph G;
vector<Vm> dp;
VV n_node;
pair<mint, int> dfs(int u, int p = -1) {
  mint res = 1;
  int res_node = 0;
  int d = G[u].size();
  dp[u] = Vm(d);
  n_node[u] = V(d);

  rep(i, d) {
    int v = G[u][i];
    if (v == p)
      continue;
    auto pa = dfs(v, u);
    dp[u][i] = pa.first;
    n_node[u][i] = pa.second;
    res *= dp[u][i];
    res_node += n_node[u][i];
  }
  res *= mint(fac[res_node]);
  rep(i, d) {
    int v = G[u][i];
    if (v == p)
      continue;
    res /= mint(fac[n_node[u][i]]);
  }
  res_node++;
  return make_pair(res, res_node);
}

Vm ans;
void dfs2(int u, mint x, int y, int p = -1) {
  int d = dp[u].size();

  rep(i, d) {
    if (G[u][i] == p) {
      dp[u][i] = x;
      n_node[u][i] = y;
    }
  }

  ans[u] = 1;
  int sum_n = 0;
  rep(i, d) {
    ans[u] *= dp[u][i];
    sum_n += n_node[u][i];
  }
  ans[u] *= mint(fac[sum_n]);
  rep(i, d) ans[u] /= mint(fac[n_node[u][i]]);

  // cout << u << ": " << endl;
  // rep(i, d) cout << dp[u][i] << endl;

  rep(i, d) {
    int v = G[u][i];
    if (v == p)
      continue;
    mint z = ans[u];
    z /= dp[u][i];
    z /= mint(binom(sum_n, n_node[u][i]));
    dfs2(v, z, sum_n + 1 - n_node[u][i], u);
  }
}

int main() {
  binom_init();
  int N;
  cin >> N;

  G = Graph(N);
  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);
  }

  dp = vector<Vm>(N);
  n_node = VV(N);
  ans = Vm(N);

  dfs(0);
  dfs2(0, 1, 0);

  rep(i, N) cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep2(i, m, n) for (int i = int(m); i < int(n); i++)
#define rep(i, n) rep2(i, 0, n)
#define all(a) a.begin(), a.end()
using ll = long long;
using ld = long double;
using V = vector<int>;
using Vll = vector<ll>;
using Vld = vector<ld>;
using VV = vector<V>;
using VVll = vector<Vll>;
using VVld = vector<Vld>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using Pld = pair<ld, ld>;
const int INF = 1 << 30;
const ll INFll = 1ll << 62;
const ld EPS = 1e-10;
const int MOD = int(1e9) + 7;
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline int Log2(T &x) {
  int k;
  for (k = 0; (T(1) << k) <= x; ++k)
    ;
  return k;
} // number of binary digits

template <int MOD> struct Fp {
  long long val;
  constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};
using mint = Fp<MOD>;
using Vm = vector<mint>;

// initialization
const int MAX = int(2e5) + 10;
long long fac[MAX], finv[MAX], inv[MAX];
void binom_init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// binom mod MOD
long long binom(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

using Graph = vector<vector<int>>;
Graph G;
vector<Vm> dp;
VV n_node;
pair<mint, int> dfs(int u, int p = -1) {
  mint res = 1;
  int res_node = 0;
  int d = G[u].size();
  dp[u] = Vm(d);
  n_node[u] = V(d);

  rep(i, d) {
    int v = G[u][i];
    if (v == p)
      continue;
    auto pa = dfs(v, u);
    dp[u][i] = pa.first;
    n_node[u][i] = pa.second;
    res *= dp[u][i];
    res_node += n_node[u][i];
  }
  res *= mint(fac[res_node]);
  rep(i, d) {
    int v = G[u][i];
    if (v == p)
      continue;
    res /= mint(fac[n_node[u][i]]);
  }
  res_node++;
  return make_pair(res, res_node);
}

Vm ans;
void dfs2(int u, mint x, int y, int p = -1) {
  int d = dp[u].size();

  int j = -1;
  if (p != -1) {
    int d2 = G[p].size();
    rep(i, d2) if (G[p][i] == u) j = i;
  }

  rep(i, d) {
    if (G[u][i] == p) {
      dp[u][i] = x / dp[p][j];
      dp[u][i] /= mint(binom(y - 1, n_node[p][j]));
      n_node[u][i] = y - n_node[p][j];
    }
  }

  ans[u] = 1;
  int sum_n = 0;
  rep(i, d) {
    ans[u] *= dp[u][i];
    sum_n += n_node[u][i];
  }
  ans[u] *= mint(fac[sum_n]);
  rep(i, d) ans[u] /= mint(fac[n_node[u][i]]);

  rep(i, d) {
    int v = G[u][i];
    if (v == p)
      continue;
    dfs2(v, ans[u], sum_n + 1, u);
  }
}

int main() {
  binom_init();
  int N;
  cin >> N;

  G = Graph(N);
  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);
  }

  dp = vector<Vm>(N);
  n_node = VV(N);
  ans = Vm(N);

  dfs(0);
  dfs2(0, 1, 0);

  rep(i, N) cout << ans[i] << endl;
  return 0;
}