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

#include <bits/stdc++.h>

#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FR(a, b) for (int i = a; i >= b; --i)
#define _upgrade                                                               \\
  cin.tie(0);                                                                  \\
  ios_base::sync_with_stdio(0)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define INF 1e9

using namespace std;

typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef pair<int, int> iPair;

const int MAX = 2e5 + 1;
const int M = 1e9 + 7;

ll n, sub[MAX], dp[MAX], f[MAX], res[MAX];
vector<int> tree[MAX];

ll mod_exp(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  if (b & 1)
    return ((a * mod_exp((a * a) % M, b / 2)) % M) % M;
  return mod_exp((a * a) % M, b / 2) % M;
}
ll inv(ll a) { return mod_exp(a, M - 2); }
ll mul(ll a, ll b) { return (a % M * b % M) % M; }
void DFS(int v, int pr) {
  sub[v] = 1;
  for (int i : tree[v])
    if (i != pr) {
      DFS(i, v);
      sub[v] += sub[i];
    }
  dp[v] = f[sub[v] - 1];
  for (int i : tree[v])
    if (i != pr)
      dp[v] = ((dp[v] * dp[i]) % M * inv(f[sub[i]]) % M) % M;
}
void solve(int v, int pr) {
  res[v] = dp[v];

  for (int i : tree[v])
    if (i != pr) {
      // make i root instead of v
      ll temp1 = dp[v], temp2 = dp[i];
      // cout<<"V: "<<dp[v]<<"\\n";

      dp[v] =
          (dp[v] * f[sub[i]]) % M; //* (f[sub[v] - sub[i] -1] //*
                                   //inv(dp[i]))%M)%M; //* inv(f[sub[v]-1]))%M;
      dp[v] = mul(dp[v], f[sub[v] - sub[i] - 1]);
      dp[v] = mul(dp[v], inv(dp[i]));
      dp[v] = mul(dp[v], inv(f[sub[v] - 1]));
      // cout<<dp[v]<<"\\n";
      dp[i] *= inv(f[sub[i] - 1]);
      dp[i] %= M;
      sub[v] -= sub[i];
      sub[i] += sub[v];
      dp[i] *= f[sub[i] - 1];
      dp[i] = ((dp[i] % M * dp[v]) % M * inv(f[sub[v]])) % M;
      solve(i, v);
      // revert changes
      dp[v] = temp1, dp[i] = temp2;
      sub[i] -= sub[v];
      sub[v] += sub[i];
    }
}
int main() {
  _upgrade;
  cin >> n;
  f[0] = 1;
  FOR(i, 1, n + 1)
  f[i] = (f[i - 1] * i) % M;

  FOR(i, 0, n - 1) {
    int a, b;
    cin >> a >> b;
    tree[a].pb(b);
    tree[b].pb(a);
  }
  DFS(1, -1);
  solve(1, -1);
  FOR(i, 1, n + 1)
  cout << res[i] << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FR(a, b) for (int i = a; i >= b; --i)
#define _upgrade                                                               \\
  cin.tie(0);                                                                  \\
  ios_base::sync_with_stdio(0)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define INF 1e9

using namespace std;

typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef pair<int, int> iPair;

const int MAX = 2e5 + 1;
const int M = 1e9 + 7;

ll n, sub[MAX], dp[MAX], f[MAX];
vector<int> tree[MAX];

ll mod_exp(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  if (b & 1)
    return (a * (mod_exp((a * a) % M, b / 2)) % M) % M;
  return mod_exp((a * a) % M, b / 2) % M;
}
ll inv(ll a) { return mod_exp(a, M - 2); }
void DFS(int v, int pr) {
  sub[v] = 1;
  for (int i : tree[v])
    if (i != pr) {
      DFS(i, v);
      sub[v] += sub[i];
    }
  dp[v] = f[sub[v] - 1];
  for (int i : tree[v])
    if (i != pr)
      dp[v] = ((dp[v] * dp[i]) % M * inv(f[sub[i]]) % M) % M;
}

int main() {
  _upgrade;
  cin >> n;
  f[0] = 1;
  FOR(i, 1, n + 1)
  f[i] = (f[i - 1] * i) % M;

  FOR(i, 0, n - 1) {
    int a, b;
    cin >> a >> b;
    tree[a].pb(b);
    tree[b].pb(a);
  }
  FOR(i, 1, n + 1) {
    DFS(i, -1);
    cout << dp[i] << "\\n";
  }
}