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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

// vector string deque break continue
#define forn(i, s, f) for (int i = (int)s; i < (int)f; i++)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fs first
#define sc second
#define pf push_front
#define pb push_back
#define pop_f pop_front
#define pop_b pop_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()

template <typename T> istream &operator>>(istream &in, vector<T> &a) {
  for (auto &i : a)
    in >> i;
  return in;
}
template <typename T> ostream &operator<<(ostream &out, vector<T> &a) {
  for (auto &i : a)
    out << i << " ";
  return out;
}
template <typename T, typename U> void chkmin(T &a, U b) {
  if (a > b)
    a = b;
}
template <typename T, typename U> void chkmax(T &a, U b) {
  if (a < b)
    a = b;
}

const int MOD = 1e9 + 7;

int add(int x, int y) {
  x += y;
  if (x >= MOD)
    return x - MOD;
  return x;
}

int sub(int x, int y) {
  x -= y;
  if (x < 0)
    return x + MOD;
  return x;
}

int mul(int x, int y) { return (1LL * x * y) % MOD; }

int bin_pow(int a, int p) {
  if (!p)
    return 1;
  if (p % 2)
    return (1LL * bin_pow(a, p - 1) * a) % MOD;
  int t = bin_pow(a, p / 2);
  return (1LL * t * t) % MOD;
}

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

vector<vector<int>> gr;
vector<int> sz, dp, f;

int C(int a, int b) {
  assert(b - a >= 0);
  return mul(f[b], mul(inv(f[a]), inv(f[b - a])));
}

int dfs(int v, int p = -1) {
  sz[v] = 1;
  dp[v] = 1;
  for (int u : gr[v]) {
    if (u == p)
      continue;
    sz[v] += dfs(u, v);
  }
  int cur = sz[v] - 1;
  for (int u : gr[v]) {
    if (u == p)
      continue;
    dp[v] = mul(dp[v], mul(C(sz[u], cur), dp[u]));
    cur -= sz[u];
  }
  return sz[v];
}

vector<int> ans;

void dfs2(int v, int p = -1) {
  ans[v] = dp[v];
  for (int u : gr[v]) {
    if (u == p)
      continue;
    dp[v] = mul(dp[v], inv(mul(C(sz[u], sz[v] - 1), dp[u])));
    sz[v] -= sz[u];
    sz[u] += sz[v];
    dp[u] = mul(dp[u], mul(C(sz[v], sz[u] - 1), dp[v]));

    dfs2(u, v);

    dp[u] = mul(dp[u], inv(mul(C(sz[v], sz[u] - 1), dp[v])));
    sz[u] -= sz[v];
    sz[v] += sz[u];
    dp[v] = mul(dp[v], mul(C(sz[u], sz[v] - 1), dp[u]));
  }
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  gr.resize(n);
  forn(i, 1, n) {
    int v, u;
    cin >> v >> u, --v, --u;
    gr[v].pb(u), gr[u].pb(v);
  }

  f.resize(n + 5);
  f[0] = 1;
  forn(i, 1, sz(f)) f[i] = mul(f[i - 1], i);
  ;

  sz = dp = vector<int>(n);
  dfs(0);

  ans = vector<int>(n);
  dfs2(0);
  for (int i : ans)
    cout << i << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

// vector string deque break continue
#define forn(i, s, f) for (int i = (int)s; i < (int)f; i++)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fs first
#define sc second
#define pf push_front
#define pb push_back
#define pop_f pop_front
#define pop_b pop_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()

template <typename T> istream &operator>>(istream &in, vector<T> &a) {
  for (auto &i : a)
    in >> i;
  return in;
}
template <typename T> ostream &operator<<(ostream &out, vector<T> &a) {
  for (auto &i : a)
    out << i << " ";
  return out;
}
template <typename T, typename U> void chkmin(T &a, U b) {
  if (a > b)
    a = b;
}
template <typename T, typename U> void chkmax(T &a, U b) {
  if (a < b)
    a = b;
}

const int MOD = 1e9 + 7;

int add(int x, int y) {
  x += y;
  if (x >= MOD)
    return x - MOD;
  return x;
}

int sub(int x, int y) {
  x -= y;
  if (x < 0)
    return x + MOD;
  return x;
}

int mul(int x, int y) { return (1LL * x * y) % MOD; }

int bin_pow(int a, int p) {
  if (!p)
    return 1;
  if (p % 2)
    return (1LL * bin_pow(a, p - 1) * a) % MOD;
  return (1LL * bin_pow(a, p / 2) * bin_pow(a, p / 2)) % MOD;
}

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

vector<vector<int>> gr;
vector<int> sz, dp, f;

int C(int a, int b) { return mul(f[b], mul(inv(f[a]), inv(f[b - a]))); }

int dfs(int v, int p = -1) {
  sz[v] = 1;
  dp[v] = 1;
  for (int u : gr[v]) {
    if (u == p)
      continue;
    sz[v] += dfs(u, v);
  }
  int cur = sz[v] - 1;
  for (int u : gr[v]) {
    if (u == p)
      continue;
    dp[v] = mul(dp[v], mul(C(sz[u], cur), dp[u]));
    cur -= sz[u];
  }
  return sz[v];
}

vector<int> ans;

void dfs2(int v, int p = -1) {
  ans[v] = dp[v];
  for (int u : gr[v]) {
    if (u == p)
      continue;
    dp[v] = mul(dp[v], inv(mul(C(sz[u], sz[v] - 1), dp[u])));
    sz[v] -= sz[u];
    sz[u] += sz[v];
    dp[u] = mul(dp[u], mul(C(sz[v], sz[u] - 1), dp[v]));

    dfs2(u, v);

    dp[u] = mul(dp[u], inv(mul(C(sz[v], sz[u] - 1), dp[v])));
    sz[u] -= sz[v];
    sz[v] += sz[u];
    dp[v] = mul(dp[v], mul(C(sz[u], sz[v] - 1), dp[u]));
  }
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  gr.resize(n);
  forn(i, 1, n) {
    int v, u;
    cin >> v >> u, --v, --u;
    gr[v].pb(u), gr[u].pb(v);
  }

  f.resize(n + 1);
  f[0] = 1;
  forn(i, 1, n + 1) f[i] = mul(f[i - 1], i);
  ;

  sz = dp = vector<int>(n);
  dfs(0);

  ans = vector<int>(n);
  dfs2(0);
  for (int i : ans)
    cout << i << "\\n";
  return 0;
}