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

#include <bits/stdc++.h>
#define int long long
#define MOD 1000000007
#define N (int)3e5
using namespace std;
int n;
struct Edge {
  int v, nxt;
} edge[N << 1];
int fst[N], ecnt;
void add_edge(int u, int v) {
  edge[ecnt].v = v, edge[ecnt].nxt = fst[u], fst[u] = ecnt++;
}
void init_e() { memset(fst, -1, sizeof(fst)); }

int ksm(int x, int y) {
  int ret = 1;
  while (y) {
    if (y & 1)
      (ret *= x) %= MOD;
    (x *= x) %= MOD;
    y >>= 1;
  }
  return ret;
}

int inv(int x) { return ksm(x, MOD - 2); }

int sz[N], dp1[N], dp2[N];
void DP1(int u, int f) {
  sz[u] = 1;
  dp1[u] = 1;
  for (int e = fst[u]; e != -1; e = edge[e].nxt) {
    int v = edge[e].v;
    if (v == f)
      continue;
    DP1(v, u);
    sz[u] += sz[v];
    (dp1[u] *= dp1[v]) %= MOD;
  }
  (dp1[u] *= sz[u]) %= MOD;
  //	cout << u << " " << dp1[u] << endl;
}
void DP2(int u, int f) {
  if (u != 1)
    dp2[u] = dp2[f] * (n - sz[u]) % MOD * inv(sz[u]) % MOD;
  //	cout << u << " " << dp2[u] << endl;
  for (int e = fst[u]; e != -1; e = edge[e].nxt) {
    int v = edge[e].v;
    if (v == f)
      continue;
    DP2(v, u);
  }
}

signed main() {
  init_e();

  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    add_edge(u, v);
    add_edge(v, u);
  }

  DP1(1, 0);
  dp2[1] = dp1[1];
  DP2(1, 0);

  int tmp = 1;
  for (int i = 1; i <= n; i++)
    (tmp *= i) %= MOD;

  for (int i = 1; i <= n; i++)
    cout << (1ll * tmp * inv(dp2[i]) % MOD) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 2e5 + 11;
const int P = 1000000007;
int f[MAXN];
int g[MAXN];
int fac[MAXN];
int inv[MAXN];
int fInv[MAXN];
int treeSize[MAXN];
vector<int> adj[MAXN];
int n;

inline int add(int x, int y) {
  x += y;
  x -= x >= P ? P : 0;
  return x;
}
inline int mul(int x, int y) { return 1ll * x * y % P; }
inline void incr(int &x, int y) { x = add(x, y); }
inline int choose(int x, int y) {
  return mul(fac[x], mul(fInv[x - y], fInv[y]));
}
inline int qPow(int a, int b) {
  int res = 1;
  int base = a;
  while (b) {
    if (b & 1)
      res = 1ll * res * base % P;
    base = 1ll * base * base % P, b >>= 1;
  }
  return res;
}
inline int Inv(int a) { return qPow(a, P - 2); }

void pre(int u, int fa) {
  treeSize[u] = 1;
  f[u] = 1;
  for (int v : adj[u]) {
    if (v == fa)
      continue;
    pre(v, u);
    treeSize[u] += treeSize[v];
    f[u] = mul(f[u], mul(f[v], choose(treeSize[u] - 1, treeSize[v])));
  }
}

void solve(int u, int fa) {
  for (int v : adj[u]) {
    if (v == fa)
      continue;
    g[v] = mul(g[u], mul(Inv(choose(n - 1, treeSize[v])),
                         choose(n - 1, treeSize[v] - 1)));
    solve(v, u);
  }
}

inline void init() {
  fac[0] = fInv[0] = 1;
  fac[1] = fInv[1] = inv[1] = 1;
  for (int i = 2; i <= n; ++i)
    fac[i] = mul(fac[i - 1], i), inv[i] = mul(P - P / i, inv[P % i]),
    fInv[i] = mul(fInv[i - 1], inv[i]);
}

int main(void) {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  cin >> n;
  init();
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  pre(1, 0);
  g[1] = f[1];
  solve(1, 0);
  for (int i = 1; i <= n; ++i)
    cout << g[i] << '\\n';
  return 0;
}