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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 61;
const ll mod = 1e9 + 7;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<ll> inv, fact, invfact;
void Mod_build(int n = 201010) {
  fact.resize(n + 1);
  inv.resize(n + 1);
  invfact.resize(n + 1);
  fact[0] = inv[0] = invfact[0] = 1;
  inv[1] = 1;
  for (ll i = 0; i < n; i++) {
    fact[i + 1] = fact[i] * (i + 1) % mod;
    if (i > 0)
      inv[i + 1] = mod - inv[mod % (i + 1)] * (mod / (i + 1)) % mod;
    invfact[i + 1] = invfact[i] * inv[i + 1] % mod;
  }
}
ll perm(int n, int k) {
  if (n < 0 || k < 0 || k > n)
    return 0;
  return fact[n] * invfact[n - k] % mod;
}
ll comb(int n, int k) {
  if (n < 0 || k < 0 || k > n)
    return 0;
  return (fact[n] * invfact[n - k] % mod) * invfact[k] % mod;
}
ll powmod(ll n, ll k) {
  k %= mod - 1;
  if (k < 0)
    k += mod - 1;
  ll ret = 1;
  while (k) {
    if (k & 1)
      ret = ret * n % mod;
    n = n * n % mod;
    k >>= 1;
  }
  return ret;
}

ll modinv(ll a) {
  ll m = mod;
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int n;
vector<int> E[201010];
VI dp1(201010);
VI dp2(201010);
VI size1(201010, 0);
VI size2(201010);
VI ans(201010);

void dfs1(int cu, int pa = -1) {
  dp1[cu] = 1;
  for (int to : E[cu]) {
    if (to != pa) {
      dfs1(to, cu);
      dp1[cu] *= invfact[size1[to]];
      dp1[cu] %= mod;
      dp1[cu] *= dp1[to];
      dp1[cu] %= mod;
      size1[cu] += size1[to];
    }
  }
  dp1[cu] *= fact[size1[cu]];
  dp1[cu] %= mod;
  size1[cu]++;
  size2[cu] = n - size1[cu];
}

void dfs2(int cu, int pa = -1) {
  if (pa == -1)
    dp2[cu] = 1;
  ll allproduct = dp2[cu] * invfact[size2[cu]] % mod;
  allproduct = allproduct * dp1[cu] % mod;
  allproduct = allproduct * invfact[size1[cu] - 1] % mod;
  ans[cu] = allproduct * fact[n - 1] % mod;
  for (int to : E[cu]) {
    if (to == pa)
      continue;
    dp2[to] = allproduct * modinv(dp1[to]);
    dp2[to] %= mod;
    dp2[to] = dp2[to] * fact[size1[to]];
    dp2[to] %= mod;
    dp2[to] = dp2[to] * fact[size2[to] - 1];
    dp2[to] %= mod;
    dfs2(to, cu);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int i, j;
  cin >> n;
  Mod_build();
  for (i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    E[a].pb(b);
    E[b].pb(a);
  }
  dfs1(0);
  dfs2(0);
  for (i = 0; i < n; i++)
    cout << ans[i] << endl;
  for (i = 0; i < n; i++) {
    // cout<<size1[i]<<" "<<dp1[i]<<" "<<dp2[i]<<endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 61;
const ll mod = 1e9 + 7;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<ll> inv, fact, invfact;
void Mod_build(int n = 201010) {
  fact.resize(n + 1);
  inv.resize(n + 1);
  invfact.resize(n + 1);
  fact[0] = inv[0] = invfact[0] = 1;
  inv[1] = 1;
  for (ll i = 0; i < n; i++) {
    fact[i + 1] = fact[i] * (i + 1) % mod;
    if (i > 0)
      inv[i + 1] = mod - inv[mod % (i + 1)] * (mod / (i + 1)) % mod;
    invfact[i + 1] = invfact[i] * inv[i + 1] % mod;
  }
}
ll perm(int n, int k) {
  if (n < 0 || k < 0 || k > n)
    return 0;
  return fact[n] * invfact[n - k] % mod;
}
ll comb(int n, int k) {
  if (n < 0 || k < 0 || k > n)
    return 0;
  return (fact[n] * invfact[n - k] % mod) * invfact[k] % mod;
}
ll powmod(ll n, ll k) {
  k %= mod - 1;
  if (k < 0)
    k += mod - 1;
  ll ret = 1;
  while (k) {
    if (k & 1)
      ret = ret * n % mod;
    n = n * n % mod;
    k >>= 1;
  }
  return ret;
}

int n;
vector<int> E[201010];
map<int, ll> v[201010];
map<int, ll> w[201010];

int calc(int cu, int pa) {
  if (v[cu].count(pa))
    return v[cu][pa];
  // v[cu][pa]=1;
  int cnt = 0;
  ll tem = 1;
  for (int to : E[cu]) {
    if (to != pa) {
      calc(to, cu);
      tem *= invfact[w[to][cu]];
      tem %= mod;
      cnt += w[to][cu];
      tem *= v[to][cu];
      tem %= mod;
    }
  }
  tem *= fact[cnt];
  tem %= mod;
  cnt++;
  w[cu][pa] = cnt;
  // assert(v[cu][pa]!=0);
  // if(v[cu][pa]<0) cout<<cu<<" "<<pa<<endl;
  return v[cu][pa] = tem;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int i, j;
  cin >> n;
  Mod_build();
  for (i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    E[a].pb(b);
    E[b].pb(a);
  }
  vector<P> q;
  for (i = 0; i < n; i++)
    q.pb(P(E[i].size(), i));
  sort(q.begin(), q.end());
  // reverse(q.begin(),q.end());
  vector<ll> ans(n);
  for (i = 0; i < n; i++) {
    ans[q[i].se] = calc(q[i].se, -1);
  }
  for (i = 0; i < n; i++) {
    printf("%d\\n", ans[i]);
  }
}