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

#include <bits/stdc++.h>
#define lld long long int
#define ld long double
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define rep(i, a, b) for (lld i = a; i <= b; i++)
#define repr(i, a, b) for (lld i = a; i >= b; i--)
#define ar array
#define pb push_back
#define mp make_pair
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
lld n, m;
lld arr[300000];
lld siz[300000];
lld fact[300000];
lld par[300000];
lld fac(lld num) {
  if (num == 0)
    return 1;
  if (fact[num] != -1)
    return fact[num];
  fact[num] = (num * fac(num - 1)) % mod;
  return fact[num];
}
lld fastpower(lld x, unsigned lld y, lld p) {
  lld res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
string st;
ar<lld, 2> dp[300000];
vector<lld> adj[300000];
void dfs_siz(lld node, lld pr) {
  siz[node] = 1;
  par[node] = pr;
  for (auto it : adj[node])
    if (it != pr) {
      dfs_siz(it, node);
      siz[node] += siz[it];
    }
}
lld dfs_dp1(lld node, lld pr) {
  if (dp[node][0] != -1)
    return dp[node][0];
  dp[node][0] = fac(siz[node] - 1);
  for (auto it : adj[node])
    if (it != pr)
      dp[node][0] = (((dp[node][0] * dfs_dp1(it, node)) % mod) *
                     fastpower(fac(siz[it]), mod - 2, mod)) %
                    mod;
  return dp[node][0];
}
lld dfs_dp2(lld node, lld pr) {
  if (dp[node][1] != -1)
    return dp[node][1];
  if (pr == -1) {
    dp[node][1] = 1;
    return dp[node][1];
  }
  dp[node][1] = (fac(n - siz[node] - 1) * dfs_dp1(pr, par[pr])) % mod;
  // for(auto it:adj[pr]) if (it!=par[pr]&&it!=node)
  // dp[node][1]=(((dp[node][1]*dfs_dp1(it,node))%mod)*fastpower(fac(siz[it]),mod-2,mod))%mod;
  dp[node][1] =
      (((((dp[node][1] * fastpower(dfs_dp1(node, pr), mod - 2, mod)) % mod) *
         fac(siz[node])) %
        mod) *
       fastpower(fac(siz[pr] - 1), mod - 2, mod)) %
      mod;
  dp[node][1] = (((dp[node][1] * dfs_dp2(pr, par[pr])) % mod) *
                 fastpower(fac(n - siz[pr]), mod - 2, mod)) %
                mod;
  return dp[node][1];
}
int main() {
  ios;
  lld TESTS, q, a, s, b, r, k, c, p, h, w, d, x, y, z, xs, ys, t;
  TESTS = 1;
  //     ans=0;
  // cin>>TESTS;
  memset(dp, -1, sizeof(dp));
  memset(fact, -1, sizeof(fact));
  while (TESTS--) {
    cin >> n;
    rep(i, 0, n - 2) {
      cin >> a >> b;
      a--;
      b--;
      adj[a].pb(b);
      adj[b].pb(a);
    }
    dfs_siz(0, -1);
    rep(i, 0, n - 1) cout
        << (((((fac(n - 1) *
                fastpower((fac(siz[i] - 1) * fac(n - siz[i])) % mod, mod - 2,
                          mod)) %
               mod) *
              dfs_dp1(i, par[i])) %
             mod) *
            dfs_dp2(i, par[i])) %
               mod
        << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define lld long long int
#define ld long double
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define rep(i, a, b) for (lld i = a; i <= b; i++)
#define repr(i, a, b) for (lld i = a; i >= b; i--)
#define ar array
#define pb push_back
#define mp make_pair
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
lld n, m;
lld arr[300000];
lld siz[300000];
lld fact[300000];
lld par[300000];
lld fac(lld num) {
  if (num == 0)
    return 1;
  if (fact[num] != -1)
    return fact[num];
  fact[num] = (num * fac(num - 1)) % mod;
  return fact[num];
}
lld fastpower(lld x, unsigned lld y, lld p) {
  lld res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
string st;
ar<lld, 2> dp[300000];
vector<lld> adj[300000];
void dfs_siz(lld node, lld pr) {
  siz[node] = 1;
  par[node] = pr;
  for (auto it : adj[node])
    if (it != pr) {
      dfs_siz(it, node);
      siz[node] += siz[it];
    }
}
lld dfs_dp1(lld node, lld pr) {
  if (dp[node][0] != -1)
    return dp[node][0];
  dp[node][0] = fac(siz[node] - 1);
  for (auto it : adj[node])
    if (it != pr)
      dp[node][0] = (((dp[node][0] * dfs_dp1(it, node)) % mod) *
                     fastpower(fac(siz[it]), mod - 2, mod)) %
                    mod;
  return dp[node][0];
}
lld dfs_dp2(lld node, lld pr) {
  if (dp[node][1] != -1)
    return dp[node][1];
  if (pr == -1) {
    dp[node][1] = 1;
    return dp[node][1];
  }
  dp[node][1] = fac(n - siz[node] - 1);
  for (auto it : adj[pr])
    if (it != par[pr] && it != node)
      dp[node][1] = (((dp[node][1] * dfs_dp1(it, node)) % mod) *
                     fastpower(fac(siz[it]), mod - 2, mod)) %
                    mod;
  dp[node][1] = (((dp[node][1] * dfs_dp2(pr, par[pr])) % mod) *
                 fastpower(fac(n - siz[pr]), mod - 2, mod)) %
                mod;
  return dp[node][1];
}
int main() {
  ios;
  lld TESTS, q, a, s, b, r, k, c, p, h, w, d, x, y, z, xs, ys, t;
  TESTS = 1;
  //     ans=0;
  // cin>>TESTS;
  memset(dp, -1, sizeof(dp));
  memset(fact, -1, sizeof(fact));
  while (TESTS--) {
    cin >> n;
    rep(i, 0, n - 2) {
      cin >> a >> b;
      a--;
      b--;
      adj[a].pb(b);
      adj[b].pb(a);
    }
    dfs_siz(0, -1);
    rep(i, 0, n - 1) cout
        << (((((fac(n - 1) *
                fastpower((fac(siz[i] - 1) * fac(n - siz[i])) % mod, mod - 2,
                          mod)) %
               mod) *
              dfs_dp1(i, par[i])) %
             mod) *
            dfs_dp2(i, par[i])) %
               mod
        << "\\n";
  }
  return 0;
}