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

#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
typedef long long ll;
typedef pair<int, ll> ill;
ll fac[200005];
ll inv[200005];
ll invfac[200005];
int p[200005];
int n;
ll SZ[200005];
ll ANS[200005];
ll ans[200005];
ll total[200005];
vector<int> adjlist[200005];
ll exp(int a, int x) {
  if (x == 0)
    return 1;
  ll p = exp(a, x / 2);
  p *= p;
  p %= mod;
  if (x & 1) {
    p *= a;
    p %= mod;
  }
  return p;
}
void dp(int u) {
  int sz = 1;
  ll ans = 1;
  for (auto v : adjlist[u]) {
    if (v == p[u])
      continue;
    p[v] = u;
    dp(v);
    sz += SZ[v];
    ans *= invfac[SZ[v]];
    ans %= mod;
    ans *= ANS[v];
    ans %= mod;
  }
  ans *= fac[sz - 1];
  ans %= mod;
  SZ[u] = sz;
  ANS[u] = ans;
}
void dp2(int u) {
  ll finalans = fac[n - 1];
  for (auto v : adjlist[u]) {
    if (v == p[u]) {
      ll newans = ans[v];
      newans *= invfac[n - 1];
      newans %= mod;
      newans *= fac[SZ[u]];
      newans %= mod;
      newans *= exp(ANS[u], mod - 2);
      newans %= mod;
      int newsz = n - SZ[u];
      newans *= fac[newsz - 1];
      newans %= mod;
      finalans *= newans;
      finalans %= mod;
      finalans *= invfac[newsz];
      finalans %= mod;
    } else {
      finalans *= ANS[v];
      finalans %= mod;
      finalans *= invfac[SZ[v]];
      finalans %= mod;
    }
  }
  ans[u] = finalans;
  for (auto v : adjlist[u]) {
    if (v == p[u])
      continue;
    dp2(v);
  }
}
int main() {
  // freopen("160f.txt","r",stdin);
  // freopen("testdebug.txt","w",stdout);
  scanf("%d", &n);
  fac[0] = inv[0] = invfac[0] = 1;
  for (int i = 1; i <= n; i++) {
    fac[i] = (fac[i - 1] * i) % mod;
    inv[i] = exp(i, mod - 2);
    invfac[i] = (invfac[i - 1] * inv[i]) % mod;
  }
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    adjlist[a].push_back(b);
    adjlist[b].push_back(a);
  }
  p[1] = 0;
  dp(1);
  dp2(1);
  for (int i = 1; i <= n; i++) {
    printf("%lld\\n", ans[i]);
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
typedef long long ll;
typedef pair<int, ll> ill;
ll fac[200005];
ll inv[200005];
int n;
unordered_map<int, int> _sz[200005];
unordered_map<int, ll> _ans[200005];
vector<int> adjlist[200005];
ll exp(int a, int x) {
  if (x == 0)
    return 1;
  ll p = exp(a, x / 2);
  p *= p;
  p %= mod;
  if (x & 1) {
    p *= a;
    p %= mod;
  }
  return p;
}
ill dp(int u, int p) {
  if (_sz[u][p] != 0)
    return {_sz[u][p], _ans[u][p]};
  // printf("%d %d\\n",u,p);
  int sz = 1;
  ll ans = 1;
  for (auto v : adjlist[u]) {
    if (v == p)
      continue;
    ill cur = dp(v, u);
    sz += cur.first;
    ans *= inv[cur.first];
    // printf("divide by fac[%d]\\n",sz[v]);
    ans %= mod;
    ans *= cur.second;
    // printf("multiply by %d\\n",ans[v]);
    ans %= mod;
  }
  ans *= fac[sz - 1];
  ans %= mod;
  _sz[u][p] = sz;
  _ans[u][p] = ans;
  return ill(sz, ans);
  // printf("%d final %d\\n",u,ans[u]);
}
int main() {
  // freopen("160f.txt","r",stdin);
  // freopen("testdebug.txt","w",stdout);
  scanf("%d", &n);
  fac[0] = inv[0] = 1;
  for (int i = 1; i <= n; i++) {
    fac[i] = (fac[i - 1] * i) % mod;
    inv[i] = exp(fac[i], mod - 2);
  }
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    adjlist[a].push_back(b);
    adjlist[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    // printf("START %d\\n",i);
    ill ans = dp(i, 0);
    printf("%lld\\n", ans.second);
  }
}