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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
class ModInt {
  static int MOD;
  long long x;

public:
  static void mod(int m) { MOD = m; }

  ModInt(long long v = 0) : x(v % MOD) {
    if (x < 0)
      x += MOD;
  }

  ModInt &operator+=(const ModInt &r) {
    x += r.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }

  ModInt &operator-=(const ModInt &r) {
    x -= r.x;
    if (x < 0)
      x += MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &r) {
    x = (x * r.x) % MOD;
    return *this;
  }
  ModInt &operator/=(const ModInt &r) {
    *this *= r.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &r) const { return ModInt(*this) += r; }
  ModInt operator-(const ModInt &r) const { return ModInt(*this) -= r; }
  ModInt operator*(const ModInt &r) const { return ModInt(*this) *= r; }
  ModInt operator/(const ModInt &r) const { return ModInt(*this) /= r; }
  bool operator==(const ModInt &r) const { return x == r.x; }
  bool operator!=(const ModInt &r) const { return x != r.x; }

  ModInt inverse() const {
    long long a = x, b = MOD, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(long long n) const {
    if (n < 0)
      return pow(-n).inverse();

    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &x) { return os << x.x; }

  friend istream &operator>>(istream &is, ModInt &x) { return is >> x.x; }

  int getMod() { return MOD; }

  long long val() { return x; }
};
int ModInt::MOD = 1000000007;

template <typename T> class Combination {
  vector<T> fact, ifact;

public:
  Combination(int nMax) : fact(nMax + 1), ifact(nMax + 1) {
    fact[0] = 1;
    for (int i = 0; i < nMax; ++i)
      fact[i + 1] = fact[i] * (i + 1);
    for (int i = 0; i < nMax + 1; ++i)
      ifact[i] = fact[i].inverse();
  }

  T factorial(int n) const { return fact[n]; }
  T inverseFactorial(int n) const { return ifact[n]; }

  T P(int n, int k) const {
    if (n < 0 || k < 0 || n < k)
      return 0;
    return factorial(n) * inverseFactorial(n - k);
  }

  T C(int n, int k) const {
    if (n < 0 || k < 0 || n < k)
      return 0;
    return factorial(n) * inverseFactorial(n - k) * inverseFactorial(k);
  }

  T H(int n, int k) const {
    if (n < 0 || k < 0)
      return 0;
    return k == 0 ? 1 : C(n + k - 1, k);
  }
};

int main() {
  int N;
  cin >> N;
  vector<vector<int>> G(N);

  struct Node {
    int i, deg;
  };
  vector<Node> nodes(N);
  for (int i = 0; i < (int)(N); ++i)
    nodes[i].i = i;
  for (int i = 0; i < (int)(N - 1); ++i) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    G[a].push_back(b);
    G[b].push_back(a);
    nodes[a].deg++;
    nodes[b].deg++;
  }

  Combination<ModInt> comb(N);

  struct State {
    int c;
    ModInt m;
  };
  unordered_map<int, unordered_map<int, State>> memo;
  function<State(int, int)> dfs = [&](int u, int p) {
    if (memo[p].find(u) != memo[p].end())
      return memo[p][u];

    State res{0, 1};
    if (p != -1 && memo[-1].find(u) != memo[-1].end()) {
      auto s = dfs(p, u);
      res.c = memo[-1][u].c - s.c;
      res.m = memo[-1][u].m / s.m / comb.factorial(memo[-1][u].c - 1) *
              comb.factorial(memo[-1][u].c - s.c - 1) * comb.factorial(s.c);
      return memo[p][u] = res;
    }

    for (int v : G[u]) {
      if (v == p)
        continue;
      auto s = dfs(v, u);
      res.c += s.c;
      res.m = res.m * s.m / comb.factorial(s.c);
    }
    res.m *= comb.factorial(res.c);
    res.c += 1;
    return memo[p][u] = res;
  };

  sort(nodes.rbegin(), nodes.rend(),
       [](const Node &a, const Node &b) { return a.deg < b.deg; });
  for (int i = 0; i < (int)(N); ++i)
    dfs(nodes[i].i, -1);

  for (int i = 0; i < (int)(N); ++i)
    cout << memo[-1][i].m << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
class ModInt {
  static int MOD;
  long long x;

public:
  static void mod(int m) { MOD = m; }

  ModInt(long long v = 0) : x(v % MOD) {
    if (x < 0)
      x += MOD;
  }

  ModInt &operator+=(const ModInt &r) {
    x += r.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }

  ModInt &operator-=(const ModInt &r) {
    x -= r.x;
    if (x < 0)
      x += MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &r) {
    x = (x * r.x) % MOD;
    return *this;
  }
  ModInt &operator/=(const ModInt &r) {
    *this *= r.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &r) const { return ModInt(*this) += r; }
  ModInt operator-(const ModInt &r) const { return ModInt(*this) -= r; }
  ModInt operator*(const ModInt &r) const { return ModInt(*this) *= r; }
  ModInt operator/(const ModInt &r) const { return ModInt(*this) /= r; }
  bool operator==(const ModInt &r) const { return x == r.x; }
  bool operator!=(const ModInt &r) const { return x != r.x; }

  ModInt inverse() const {
    long long a = x, b = MOD, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(long long n) const {
    if (n < 0)
      return pow(-n).inverse();

    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &x) { return os << x.x; }

  friend istream &operator>>(istream &is, ModInt &x) { return is >> x.x; }

  int getMod() { return MOD; }

  long long val() { return x; }
};
int ModInt::MOD = 1000000007;

template <typename T> class Combination {
  vector<T> fact, ifact;

public:
  Combination(int nMax) : fact(nMax + 1), ifact(nMax + 1) {
    fact[0] = 1;
    for (int i = 0; i < nMax; ++i)
      fact[i + 1] = fact[i] * (i + 1);
    for (int i = 0; i < nMax + 1; ++i)
      ifact[i] = fact[i].inverse();
  }

  T factorial(int n) const { return fact[n]; }
  T inverseFactorial(int n) const { return ifact[n]; }

  T P(int n, int k) const {
    if (n < 0 || k < 0 || n < k)
      return 0;
    return factorial(n) * inverseFactorial(n - k);
  }

  T C(int n, int k) const {
    if (n < 0 || k < 0 || n < k)
      return 0;
    return factorial(n) * inverseFactorial(n - k) * inverseFactorial(k);
  }

  T H(int n, int k) const {
    if (n < 0 || k < 0)
      return 0;
    return k == 0 ? 1 : C(n + k - 1, k);
  }
};

int main() {
  int N;
  cin >> N;
  vector<vector<int>> G(N);

  for (int i = 0; i < (int)(N - 1); ++i) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  Combination<ModInt> comb(N);

  struct State {
    int c;
    ModInt m;
  };
  unordered_map<int, unordered_map<int, State>> memo;
  function<State(int, int)> dfs = [&](int u, int p) {
    if (memo[p].find(u) != memo[p].end())
      return memo[p][u];

    State res{0, 1};
    if (p != -1 && memo[-1].find(u) != memo[-1].end()) {
      auto s = dfs(p, u);
      res.c = memo[-1][u].c - s.c;
      res.m = memo[-1][u].m / s.m / comb.factorial(memo[-1][u].c - 1) *
              comb.factorial(memo[-1][u].c - s.c - 1) * comb.factorial(s.c);
      return memo[p][u] = res;
    }

    for (int v : G[u]) {
      if (v == p)
        continue;
      auto s = dfs(v, u);
      res.c += s.c;
      res.m = res.m * s.m / comb.factorial(s.c);
    }
    res.m *= comb.factorial(res.c);
    res.c += 1;
    return memo[p][u] = res;
  };

  for (int i = 0; i < (int)(N); ++i)
    cout << dfs(i, -1).m << endl;

  return 0;
}