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

#include <bits/stdc++.h>
#define int long long
#define ll long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define ALL(a) begin(a), end(a)
#define RALL(a) rbegin(a), rend(a)
#define SORT(a) stable_sort(ALL(a))
#define RSORT(a) stable_sort(RALL(a))
#define REV(a) reverse(ALL(a))
#define UNIQUE(a) a.erase(unique(ALL(a)), end(a))
#define lambda(i) [=](auto i)
#define compare(i, j) [=](auto i, auto j)
#define min_of(a) *min_element(ALL(a))
#define max_of(a) *max_element(ALL(a))
#define sum(a) accumulate(ALL(a), 0)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define SZ(a) (int)(a).size()
#define REP(i, n) for (decltype(+n) i = 0; i < (n); i++)
#define RREP(i, n) for (auto i = n - 1; i != static_cast<decltype(i)>(-1); i--)
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define RFOR(i, a, b) for (auto i = b - 1; i != a; i--)
#define EACH(i, a) for (auto &&i : (a))
using namespace std;

const int INF = 1e18;
const int MOD = 1e9 + 7;
int mod_add(int A, int B) { return (A + B) % MOD; }
int mod_sub(int A, int B) { return (A + MOD - B) % MOD; }
int mod_mul(int A, int B) { return ((A % MOD) * (B % MOD)) % MOD; }
int mod_bipow(int X, int Y) {
  if (Y == 0)
    return 1;
  else if (Y == 1)
    return X % MOD;
  else if (Y % 2 == 0) {
    int Z = mod_bipow(X, (int)(Y / 2));
    return mod_mul(Z, Z);
  } else {
    int Z = mod_bipow(X, (int)(Y / 2));
    return mod_mul(mod_mul(Z, Z), X);
  }
}
int mod_inv(int X) { return mod_bipow(X, MOD - 2); }
int mod_div(int A, int B) { return mod_mul(A, mod_inv(B)); }

vi fact;

struct vertex {
  vector<int> adj = vi(0);
  int dp = 1;
  int p_dp = 1;
  int p_s = 1;
  int s = 1;
  void merge(vertex vt) {
    p_dp = mod_mul(p_dp, vt.dp);
    p_s = mod_mul(p_s, fact[vt.s]);
    s += vt.s;
    update_dp();
  };
  void perge(vertex vt) {
    p_dp = mod_div(p_dp, vt.dp);
    p_s = mod_div(p_s, fact[vt.s]);
    s -= vt.s;
    update_dp();
  };
  void update_dp() { dp = mod_div(mod_mul(p_dp, fact[s - 1]), p_s); }
};

int n;
vector<vertex> v;

void dfs(int x, int p) {
  EACH(i, v[x].adj) {
    if (i == p)
      continue;
    dfs(i, x);
    v[x].merge(v[i]);
  }
}

void r_dfs(int x, int p) {
  EACH(i, v[x].adj) {
    if (i == p)
      continue;
    int p_dp_x = mod_div(v[x].p_dp, v[i].dp);
    int p_s_x = mod_div(v[x].p_s, fact[v[i].s]);
    int s_x = v[x].s - v[i].s;
    int dp_x = mod_div(mod_mul(p_dp_x, fact[s_x - 1]), p_s_x);
    v[i].p_dp = mod_mul(v[i].p_dp, dp_x);
    v[i].p_s = mod_mul(v[i].p_s, fact[s_x]);
    v[i].s += s_x;
    v[i].update_dp();
    r_dfs(i, x);
  }
}

void solve() {
  cin >> n;
  fact = vi(n, 1);
  v = vector<vertex>(n);
  FOR(i, 1, n) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].adj.pb(b);
    v[b].adj.pb(a);
    fact[i] = mod_mul(fact[i - 1], i);
  }
  dfs(0, -1);
  r_dfs(0, -1);
  EACH(i, v) { cout << i.dp << endl; }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(15);
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define ll long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define ALL(a) begin(a), end(a)
#define RALL(a) rbegin(a), rend(a)
#define SORT(a) stable_sort(ALL(a))
#define RSORT(a) stable_sort(RALL(a))
#define REV(a) reverse(ALL(a))
#define UNIQUE(a) a.erase(unique(ALL(a)), end(a))
#define lambda(i) [=](auto i)
#define compare(i, j) [=](auto i, auto j)
#define min_of(a) *min_element(ALL(a))
#define max_of(a) *max_element(ALL(a))
#define sum(a) accumulate(ALL(a), 0)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define SZ(a) (int)(a).size()
#define REP(i, n) for (decltype(+n) i = 0; i < (n); i++)
#define RREP(i, n) for (auto i = n - 1; i != static_cast<decltype(i)>(-1); i--)
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define RFOR(i, a, b) for (auto i = b - 1; i != a; i--)
#define EACH(i, a) for (auto &&i : (a))
using namespace std;

const int INF = 1e18;
const int MOD = 1e9 + 7;
int mod_add(int A, int B) { return (A + B) % MOD; }
int mod_sub(int A, int B) { return (A + MOD - B) % MOD; }
int mod_mul(int A, int B) { return ((A % MOD) * (B % MOD)) % MOD; }
int mod_bipow(int X, int Y) {
  if (Y == 0)
    return 1;
  else if (Y == 1)
    return X % MOD;
  else if (Y % 2 == 0) {
    int Z = mod_bipow(X, (int)(Y / 2));
    return mod_mul(Z, Z);
  } else {
    int Z = mod_bipow(X, (int)(Y / 2));
    return mod_mul(mod_mul(Z, Z), X);
  }
}
int mod_inv(int X) { return mod_bipow(X, MOD - 2); }
int mod_div(int A, int B) { return mod_mul(A, mod_inv(B)); }

vi fact;

struct vertex {
  vector<int> adj = vi(0);
  int dp = 1;
  int p_dp = 1;
  int p_s = 1;
  int s = 1;
  void merge(vertex vt) {
    p_dp = mod_mul(p_dp, vt.dp);
    p_s = mod_mul(p_s, fact[vt.s]);
    s += vt.s;
  };
  void perge(vertex vt) {
    p_dp = mod_div(p_dp, vt.dp);
    p_s = mod_div(p_s, fact[vt.s]);
    s -= vt.s;
  };
  void update_dp() { dp = mod_div(mod_mul(p_dp, fact[s - 1]), p_s); }
};

int n;
vector<vertex> v;

void dfs(int x, int p) {
  EACH(i, v[x].adj) {
    if (i == p)
      continue;
    dfs(i, x);
    v[x].merge(v[i]);
  }
  v[x].update_dp();
}

void r_dfs(int x, int p) {
  EACH(i, v[x].adj) {
    if (i == p)
      continue;
    vertex tmp = v[x];
    tmp.perge(v[i]);
    tmp.update_dp();
    v[i].merge(tmp);
    v[i].update_dp();
    r_dfs(i, x);
  }
}

void solve() {
  cin >> n;
  fact = vi(n, 1);
  v = vector<vertex>(n);
  FOR(i, 1, n) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].adj.pb(b);
    v[b].adj.pb(a);
    fact[i] = mod_mul(fact[i - 1], i);
  }
  dfs(0, -1);
  r_dfs(0, -1);
  EACH(i, v) { cout << i.dp << endl; }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(15);
  solve();
}