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

#include <bits/stdc++.h>
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr << #a << "=" << a << endl
#define dumparr(a, n) cerr << #a << "[" << n << "]=" << a[n] << endl
#else
#define dump(a)
#define dumparr(a, n)
#endif
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for (ll i = (ll)b - 1LL; i >= (ll)a; i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define SIGN(a) (a == 0 ? 0 : (a > 0 ? 1 : -1))

typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> ppll;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ld, ld> pdd;

const ll INF = (1LL << 50);
#if __cplusplus < 201700L
ll gcd(ll a, ll b) {
  if (a < b)
    return gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
#endif
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class S, class T>
std::ostream &operator<<(std::ostream &os, pair<S, T> a) {
  os << "(" << a.first << "," << a.second << ")";
  return os;
}
template <class T> std::ostream &operator<<(std::ostream &os, vector<T> a) {
  os << "[ ";
  REP(a.size()) { os << a[i] << " "; }
  os << " ]";
  return os;
}

const long long MOD = 1000000007;
ll cached[300000];

vector<vector<ll>> tree;
const long long max_cache = 300000;
long long cache_fact[max_cache], cache_inv_fact[max_cache];
long long combination_cached(long long n, long long r) {
  ll m = MOD;
  if (r < 0 || r > n)
    return 0;
  if (r == 0 || r == n)
    return 1;
  if (n > m) {
    std::cerr << "Combination Error: n is greater than m" << std::endl;
    exit(1);
  }
  static bool cached = false;
  if (cached == false) {
    cached = true;
    cache_inv_fact[1] = 1;
    for (long long i = 2; i < max_cache; i++) {
      cache_inv_fact[i] = (m - m / i) * cache_inv_fact[m % i] % m;
    }
    for (long long i = 2; i < max_cache; i++) {
      cache_inv_fact[i] = cache_inv_fact[i] * cache_inv_fact[i - 1] % m;
    }
    cache_fact[0] = 1;
    for (long long i = 1; i < max_cache; i++) {
      cache_fact[i] = cache_fact[i - 1] * i % m;
    }
  }
  r = min(r, n - r);
  return cache_fact[n] * cache_inv_fact[r] % m * cache_inv_fact[n - r] % m;
}
vector<map<ll, pll>> dfs_cache;
vector<ll> ans;
pll dfs1(ll parent, ll cur) {
  pll ret = pll(1, 0);
  vector<pll> stack;
  for (auto &&i : tree[cur]) {
    if (i == parent)
      continue;
    auto tmp = dfs1(cur, i);
    dfs_cache[cur][i] = tmp;
    ret.second += tmp.second;
    stack.push_back(tmp);
  }
  ll rest = ret.second;
  for (auto &&i : stack) {
    ret.first =
        ret.first * combination_cached(rest, i.second) % MOD * i.first % MOD;
    rest -= i.second;
  }
  ret.second++;
  if (parent == -1) {
    ans[cur] = ret.first;
  }
  return ret;
}
template <typename T> T modpow(T a, unsigned long long b) {
  T base = a, ans = T(1LL);
  while (b) {
    if (b & 1) {
      ans = ans * base % MOD;
    }
    base = base * base % MOD;
    b >>= 1;
  }
  return ans;
}
template <typename T> T invpow(T a) { return modpow(a, MOD - 2); }
void dfs2(ll parent, ll cur, pll num = pll(0, 0)) {
  if (parent == -1) {
  } else {
    dump(pll(parent, cur));
    dump(num);
    dfs_cache[cur][parent] = num;
    ans[cur] = dfs_cache[parent][cur].first *
               combination_cached(tree.size() - 1, num.second) % MOD *
               num.first % MOD;
  }
  for (auto &&i : tree[cur]) {
    if (i == parent)
      continue;
    ll childnum =
        ans[cur] *
        invpow(combination_cached(tree.size() - 1, dfs_cache[cur][i].second) *
               dfs_cache[cur][i].first % MOD) %
        MOD;
    dfs2(cur, i, pll(childnum, tree.size() - dfs_cache[cur][i].second));
  }
}
void solve(long long N, std::vector<long long> a, std::vector<long long> b) {
  tree.resize(N);
  dfs_cache.resize(N);
  ans.resize(N);
  cached[0] = 1;
  REP(N) { cached[i + 1] = cached[i] * (i + 1) % MOD; }
  REP(N - 1) {
    tree[a[i]].push_back(b[i]);
    tree[b[i]].push_back(a[i]);
  }
  dfs1(-1, 0);
  dfs2(-1, 0);
  REP(N) { cout << ans[i] << endl; }
}

int main() {
  cout << setprecision(1000);
  long long N;
  scanf("%lld", &N);
  std::vector<long long> a(N - 1);
  std::vector<long long> b(N - 1);
  for (int i = 0; i < N - 1; i++) {
    scanf("%lld", &a[i]);
    scanf("%lld", &b[i]);
    --a[i];
    --b[i];
  }
  solve(N, std::move(a), std::move(b));
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr << #a << "=" << a << endl
#define dumparr(a, n) cerr << #a << "[" << n << "]=" << a[n] << endl
#else
#define dump(a)
#define dumparr(a, n)
#endif
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for (ll i = (ll)b - 1LL; i >= (ll)a; i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define SIGN(a) (a == 0 ? 0 : (a > 0 ? 1 : -1))

typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> ppll;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ld, ld> pdd;

const ll INF = (1LL << 50);
#if __cplusplus < 201700L
ll gcd(ll a, ll b) {
  if (a < b)
    return gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
#endif
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class S, class T>
std::ostream &operator<<(std::ostream &os, pair<S, T> a) {
  os << "(" << a.first << "," << a.second << ")";
  return os;
}
template <class T> std::ostream &operator<<(std::ostream &os, vector<T> a) {
  os << "[ ";
  REP(a.size()) { os << a[i] << " "; }
  os << " ]";
  return os;
}

const long long MOD = 1000000007;
ll cached[300000];

vector<vector<ll>> tree;
const long long max_cache = 300000;
long long cache_fact[max_cache], cache_inv_fact[max_cache];
long long combination_cached(long long n, long long r) {
  ll m = MOD;
  if (r < 0 || r > n)
    return 0;
  if (r == 0 || r == n)
    return 1;
  if (n > m) {
    std::cerr << "Combination Error: n is greater than m" << std::endl;
    exit(1);
  }
  static bool cached = false;
  if (cached == false) {
    cached = true;
    cache_inv_fact[1] = 1;
    for (long long i = 2; i < max_cache; i++) {
      cache_inv_fact[i] = (m - m / i) * cache_inv_fact[m % i] % m;
    }
    for (long long i = 2; i < max_cache; i++) {
      cache_inv_fact[i] = cache_inv_fact[i] * cache_inv_fact[i - 1] % m;
    }
    cache_fact[0] = 1;
    for (long long i = 1; i < max_cache; i++) {
      cache_fact[i] = cache_fact[i - 1] * i % m;
    }
  }
  r = min(r, n - r);
  return cache_fact[n] * cache_inv_fact[r] % m * cache_inv_fact[n - r] % m;
}
map<pll, pll> ans_cache;
pll answer(ll parent, ll cur) {
  if (ans_cache.count(pll(parent, cur))) {
    return ans_cache[pll(parent, cur)];
  }
  pll ret = pll(1, 0);
  vector<pll> stack;
  for (auto &&i : tree[cur]) {
    if (i == parent)
      continue;
    auto tmp = answer(cur, i);
    ret.second += tmp.second;
    stack.push_back(tmp);
  }
  ll rest = ret.second;
  for (auto &&i : stack) {
    ret.first =
        ret.first * combination_cached(rest, i.second) % MOD * i.first % MOD;
    rest -= i.second;
  }
  ret.second++;
  ans_cache[pll(parent, cur)] = ret;
  return ret;
}
void solve(long long N, std::vector<long long> a, std::vector<long long> b) {
  tree.resize(N);
  cached[0] = 1;
  REP(N) { cached[i + 1] = cached[i] * (i + 1) % MOD; }
  REP(N - 1) {
    tree[a[i]].push_back(b[i]);
    tree[b[i]].push_back(a[i]);
  }
  For(k, N) { cout << answer(-1, k).first << endl; }
}

int main() {
  cout << setprecision(1000);
  long long N;
  scanf("%lld", &N);
  std::vector<long long> a(N - 1);
  std::vector<long long> b(N - 1);
  for (int i = 0; i < N - 1; i++) {
    scanf("%lld", &a[i]);
    scanf("%lld", &b[i]);
    --a[i];
    --b[i];
  }
  solve(N, std::move(a), std::move(b));
  return 0;
}