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

#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
// using arr = array;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using vpi = vector<pi>;
using ld = long double;
#include <bits/extc++.h>
using namespace __gnu_pbds;
using oset = tree<ll, null_type, less<ll>, rb_tree_tag,
                  tree_order_statistics_node_update>;
const int maxn = 1 << 18, mod = 1e9 + 7, lg = 19;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int fact[maxn], inv[maxn];
void setup() {
  fact[0] = fact[1] = inv[0] = inv[1] = 1;
  for (int i = 2; i < maxn; i++)
    inv[i] = mod - (mod / i) * 1ll * inv[mod % i] % mod;
  for (int i = 2; i < maxn; i++)
    inv[i] = inv[i - 1] * 1ll * inv[i] % mod,
    fact[i] = fact[i - 1] * 1ll * i % mod;
}
int bp(int a, int p) {
  int r = 1;
  while (p) {
    if (p & 1)
      r = r * 1ll * a % mod;
    p >>= 1, a = a * 1ll * a % mod;
  }
  return r;
}
struct ctx {
  int sz, val;
  ctx() : sz(1), val(1) {}
  void root() {
    val = get() * 1ll * inv[sz] % mod;
    sz++;
  }
  int get() { return val * 1ll * fact[sz - 1] % mod; }
  void add(ctx a) {
    val = val * 1ll * a.get() % mod;
    val = val * 1ll * inv[a.sz] % mod;
    sz += a.sz;
  }
  void rem(ctx a) {
    val = val * 1ll * bp(a.get(), mod - 2) % mod;
    val = val * 1ll * fact[a.sz] % mod;
    sz -= a.sz;
  }
};
ctx dp[maxn];
vi g[maxn];
int n, ans[maxn];
void calc(int v, int p, ctx cur) {
  for (auto &i : g[v])
    if (i != p)
      cur.add(dp[i]);
  ans[v] = cur.get();
  for (auto &i : g[v]) {
    if (i == p)
      continue;
    ctx t = cur;
    t.rem(dp[i]);
    t.root();
    calc(i, v, t);
  }
}
void dfs(int v, int p) {
  for (auto i : g[v])
    if (i != p)
      dfs(i, v), dp[v].add(dp[i]);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  setup();
  cin >> n;
  for (int f, t, i = 1; i < n; i++) {
    cin >> f >> t;
    g[f].pb(t);
    g[t].pb(f);
  }
  dfs(1, 1);
  calc(1, 1, ctx());
  for (int i = 1; i <= n; i++)
    cout << ans[i] << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

// #pragma GCC optimize("O2")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx,avx2,sse,sse2,ssse3,tune=native")
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
using vi = vector<int>;
using pi = pair<int, int>;
using vpi = vector<pi>;
const int maxn = 3e5 + 55, mod = 1e9 + 7;
int fact[maxn], inv[maxn];
void setup() {
  fact[0] = fact[1] = inv[0] = inv[1] = 1;
  for (int i = 2; i < maxn; i++)
    inv[i] = mod - (mod / i) * 1ll * inv[mod % i] % mod;
  for (int i = 2; i < maxn; i++)
    inv[i] = inv[i] * 1ll * inv[i - 1] % mod,
    fact[i] = fact[i - 1] * 1ll * i % mod,
    assert(fact[i] * 1ll * inv[i] % mod == 1);
}
int multi(int n, vi &x) {
  int ans = fact[n];
  for (auto i : x) {
    ans = ans * 1ll * inv[i] % mod;
  }
  return ans;
}
int n;
vi g[maxn];
map<pi, int> dp, sz;

void dfs(int v, int p) {
  if (dp.count({v, p}))
    return;
  vi s;
  sz[{v, p}] = 1;
  dp[{v, p}] = 1;
  for (auto i : g[v]) {
    if (i == p)
      continue;
    dfs(i, v);
    sz[{v, p}] += sz[{i, v}];
    dp[{v, p}] = dp[{v, p}] * 1ll * dp[{i, v}] % mod;
    s.pb(sz[{i, v}]);
  }
  dp[{v, p}] = dp[{v, p}] * 1ll * multi(sz[{v, p}] - 1, s) % mod;
  // cout << v << " " << p << " " << dp[{v, p}] << '\\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  setup();
  cin >> n;
  for (int t, f, i = 1; i < n; i++) {
    cin >> f >> t;
    g[f].pb(t);
    g[t].pb(f);
  }
  for (int i = 1; i <= n; i++)
    dfs(i, i), cout << dp[{i, i}] << '\\n';
}