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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Pll = pair<ll, ll>;
constexpr int MOD = 1000000007;

int main() {
  int N;
  cin >> N;
  vector<vector<int>> G(N);
  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    cin >> a >> b;
    G[a - 1].push_back(b - 1);
    G[b - 1].push_back(a - 1);
  }
  vector<ll> fact(N), factinv(N), inv(N);
  fact[0] = fact[1] = 1;
  factinv[0] = factinv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < N; ++i) {
    fact[i] = fact[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    factinv[i] = factinv[i - 1] * inv[i] % MOD;
  }
  function<ll(ll)> modinv = [&](ll x) {
    ll b = MOD, u = 1, v = 0;
    while (b) {
      ll t = x / b;
      x -= t * b;
      u -= t * v;
      swap(x, b);
      swap(u, v);
    }
    return (u + MOD) % MOD;
  };
  vector<map<int, Pll>> dp(N);
  function<Pll(int, int)> dfs = [&](int u, int par) {
    if (!dp[u].count(par)) {
      Pll ret = {1, 1};
      if (!dp[u].count(-1)) {
        for (int v : G[u]) {
          if (v != par) {
            Pll t = dfs(v, u);
            ret.first += t.first;
            ret.second = ret.second * t.second % MOD;
            ret.second = ret.second * factinv[t.first] % MOD;
          }
        }
        ret.second = ret.second * fact[ret.first - 1] % MOD;
      } else {
        ret = dp[u][-1];
        Pll t = dfs(par, u);
        ret.second = ret.second * factinv[ret.first - 1] % MOD;
        ret.second = ret.second * fact[t.first] % MOD;
        ret.first -= t.first;
        ret.second = ret.second * fact[ret.first - 1] % MOD;
        ret.second = ret.second * modinv(t.second) % MOD;
      }
      dp[u][par] = ret;
    }
    return dp[u][par];
  };
  vector<ll> id(N), ans(N);
  iota(id.begin(), id.end(), 0);
  sort(id.begin(), id.end(),
       [&](const int &l, const int &r) { return G[l].size() > G[r].size(); });
  for (int i = 0; i < N; ++i)
    ans[id[i]] = dfs(id[i], -1).second;
  for (int i = 0; i < N; ++i)
    cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Pll = pair<ll, ll>;
constexpr int MOD = 1000000007;

int main() {
  int N;
  cin >> N;
  vector<vector<int>> G(N);
  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    cin >> a >> b;
    G[a - 1].push_back(b - 1);
    G[b - 1].push_back(a - 1);
  }
  vector<ll> fact(N), factinv(N), inv(N);
  fact[0] = fact[1] = 1;
  factinv[0] = factinv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < N; ++i) {
    fact[i] = fact[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    factinv[i] = factinv[i - 1] * inv[i] % MOD;
  }
  function<ll(ll)> modinv = [&](ll x) {
    ll b = MOD, u = 1, v = 0;
    while (b) {
      ll t = x / b;
      x -= t * b;
      u -= t * v;
      swap(x, b);
      swap(u, v);
    }
    return (u + MOD) % MOD;
  };
  vector<map<int, Pll>> dp(N);
  function<Pll(int, int)> dfs = [&](int u, int par) {
    if (!dp[u].count(par)) {
      Pll ret = {1, 1};
      if (!dp[u].count(-1)) {
        for (int v : G[u]) {
          if (v != par) {
            Pll t = dfs(v, u);
            ret.first += t.first;
            ret.second = ret.second * t.second % MOD;
            ret.second = ret.second * factinv[t.first] % MOD;
          }
        }
        ret.second = ret.second * fact[ret.first - 1] % MOD;
      } else {
        ret = dp[u][-1];
        Pll t = dfs(par, u);
        ret.second = ret.second * factinv[ret.first - 1] % MOD;
        ret.second = ret.second * fact[t.first] % MOD;
        ret.first -= t.first;
        ret.second = ret.second * fact[ret.first - 1] % MOD;
        ret.second = ret.second * modinv(t.second) % MOD;
      }
      dp[u][par] = ret;
    }
    return dp[u][par];
  };
  vector<ll> id(N), ans(N);
  iota(id.begin(), id.end(), 0);
  sort(id.begin(), id.end(),
       [&](const int &l, const int &r) { return G[l] > G[r]; });
  for (int i = 0; i < N; ++i)
    ans[id[i]] = dfs(id[i], -1).second;
  for (int i = 0; i < N; ++i)
    cout << ans[i] << endl;
  return 0;
}