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

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
const ll inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll MAX = 200005;
vector<ll> G[MAX];
vector<ll> ans(MAX);
vector<ll> siz(MAX);
ll rui(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
void preDFS(ll u, ll p) {
  for (int to : G[u]) {
    if (to == p)
      continue;
    preDFS(to, u);
    siz[u] += siz[to];
  }
  siz[u]++;
}
ll sum = 1;
void DFS(ll u, ll p) {
  ans[u] = sum;
  for (int to : G[u]) {
    if (to == p)
      continue;
    ll a = siz[u], b = siz[to], c = sum;
    sum *= siz[to];
    sum %= mod;
    siz[u] -= siz[to];
    sum *= rui(siz[u], mod - 2);
    sum %= mod;
    siz[to] = a;
    DFS(to, u);
    siz[u] = a;
    siz[to] = b;
    sum = c;
  }
}
int main() {
  ll n;
  cin >> n;
  rep(i, n - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].pb(b);
    G[b].pb(a);
  }
  preDFS(0, -1);
  REP(i, 1, n) {
    sum *= i;
    sum %= mod;
  }
  REP(i, 1, n) {
    sum *= rui(siz[i], mod - 2);
    sum %= mod;
  }
  DFS(0, -1);
  rep(i, n) { cout << ans[i] << "\\n"; }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
const ll inf = 1e9 + 7;
const ll mod = 1e9 + 7;
long long extGCD(long long a, long long mod, long long &x, long long &y) {
  if (mod == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long d = extGCD(mod, a % mod, y, x);
  y -= (a / mod) * x;
  return d;
}
long long invmod(long long a, long long mod) {
  long long x, y;
  extGCD(a, mod, x, y);
  x %= mod;
  if (x < 0)
    x += mod;
  return x;
}
ll fac[2000010], finv[2000010], inv[2000010];
void combinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  REP(i, 2, 2000010) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = invmod(i, mod);
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}
ll comb(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
vector<vector<ll>> g(200010);
map<pll, ll> dp;
map<pll, ll> sz;
ll dfs(ll p, ll i) {
  if (sz.find(mp(p, i)) != sz.end())
    return sz[mp(p, i)];
  ll res = 1;
  for (auto e : g[i]) {
    if (p == e)
      continue;
    res += dfs(i, e);
  }
  sz[mp(p, i)] = res;
  return res;
}
ll dfs2(ll p, ll i) {
  if (dp.find(mp(p, i)) != dp.end())
    return dp[mp(p, i)];
  ll res1 = 1;
  ll res2 = 1;
  ll cnt = 0;
  for (auto e : g[i]) {
    if (p == e)
      continue;
    cnt += sz[mp(i, e)];
    res1 *= comb(cnt, sz[mp(i, e)]);
    res1 %= inf;
    res2 *= dfs2(i, e);
    res2 %= inf;
  }
  if (cnt > 1)
    res1 = res1 % inf * res2 % inf;
  else
    res1 = res2;
  dp[mp(p, i)] = res1;
  return res1;
}
int main() {
  combinit();
  sz.clear();
  dp.clear();
  ll n;
  cin >> n;
  rep(i, n - 1) {
    ll a, b;
    cin >> a >> b;
    g[a].pb(b);
    g[b].pb(a);
  }
  rep(i, n) {
    dfs(0, i + 1);
    ll ans = dfs2(0, i + 1);
    ans %= inf;
    if (ans < 0)
      ans += inf;
    cout << ans << endl;
  }
  rep(i, n) {
    rep(j, n) {
      if (i == j)
        continue;
      // cout<<i+1<<' '<<j+1<<' '<<dp[mp(i+1,j+1)]<<endl;
    }
  }
}