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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vl = vector<ll>;
using Map = map<P, ll>;
using T = tuple<ll, ll, ll>;
using vvl = vector<vector<ll>>;
#define all(v) v.begin(), v.end()
#define prt(v) cout << v << "\\n";
#define fi(v) get<0>(v)
#define se(v) get<1>(v)
#define th(v) get<2>(v)
#define endl "\\n"
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1000000007;

#define MAX 200003
long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void fact() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < MAX; ++i) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long 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;
}

ll N;

vector<vector<ll>> G(200001, vector<ll>(0));

Map dp, dp2;

ll R(P par) {
  if (dp2[par])
    return dp2[par];
  ll x = par.first;
  ll y = par.second;
  if (dp2[P(y, x)])
    return dp2[par] = N - dp2[P(y, x)];
  if (x == 0)
    return 0;
  ll ret = 1;
  for (auto p : G[x])
    if (p != y)
      ret += R(P(p, x));
  return dp2[par] = ret;
}

ll F(P par) {
  if (dp[par])
    return dp[par];
  ll x = par.first;
  ll y = par.second;
  ll ret = 1;
  ll sum = N - 1 - R(P(y, x));
  for (auto p : G[x]) {
    if (p == y)
      continue;
    ll t = R(P(p, x));
    ret *= comb(sum, t);
    sum -= t;
    ret %= MOD;
  }
  for (auto p : G[x]) {
    if (p == y)
      continue;
    ret *= F(P(p, x));
    ret %= MOD;
  }
  return dp[par] = ret;
}

signed main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  fact();
  cin >> N;
  vector<ll> A(N - 1, 0);
  vector<ll> B(N - 1, 0);
  for (ll i = 0; i < N - 1; ++i)
    cin >> A[i] >> B[i];
  for (ll i = 0; i < N - 1; ++i) {
    G[A[i]].emplace_back(B[i]);
    G[B[i]].emplace_back(A[i]);
  }
  map<ll, ll> M;
  for (ll i = 1; i <= N; ++i)
    M[G[i].size()]++;

  // ウニ対策
  if (M[N - 1]) {
    for (ll i = 1; i <= N; ++i) {
      if (G[i].size() == 1)
        prt(fac[N - 2]) else prt(fac[N - 1])
    }
    return 0;
  }

  for (ll i = 1; i <= N; ++i)
    prt(F(P(i, 0)))

        return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vl = vector<ll>;
using Map = map<P, ll>;
using T = tuple<ll, ll, ll>;
using vvl = vector<vector<ll>>;
#define all(v) v.begin(), v.end()
#define prt(v) cout << v << "\\n";
#define fi(v) get<0>(v)
#define se(v) get<1>(v)
#define th(v) get<2>(v)
#define endl "\\n"
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1000000007;

#define MAX 200003
long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void fact() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < MAX; ++i) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long 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;
}
// ↓↓↓↓↓↓fact()ってちゃんとつける！！！！！！！！！！！↓↓↓↓↓↓
// ↓↓↓↓↓↓fact()ってちゃんとつける！！！！！！！！！！！↓↓↓↓↓↓
// ↓↓↓↓↓↓fact()ってちゃんとつける！！！！！！！！！！！↓↓↓↓↓↓
// ↓↓↓↓↓↓fact()ってちゃんとつける！！！！！！！！！！！↓↓↓↓↓↓
// ↓↓↓↓↓↓fact()ってちゃんとつける！！！！！！！！！！！↓↓↓↓↓↓

ll N;

vector<vector<ll>> G(200001, vector<ll>(0));

Map dp, dp2;

ll R(P par) {
  if (dp2[par])
    return dp2[par];
  ll x = par.first;
  ll y = par.second;
  if (x == 0)
    return 0;
  ll ret = 1;
  for (auto p : G[x])
    if (p != y)
      ret += R(P(p, x));
  return dp2[par] = ret;
}

ll F(P par) {
  if (dp[par])
    return dp[par];
  ll x = par.first;
  ll y = par.second;
  ll ret = 1;
  ll sum = N - 1 - R(P(y, x));
  for (auto p : G[x]) {
    if (p == y)
      continue;
    ret *= comb(sum, R(P(p, x)));
    sum -= R(P(p, x));
    ret %= MOD;
  }
  for (auto p : G[x]) {
    if (p == y)
      continue;
    ret *= F(P(p, x));
    ret %= MOD;
  }
  return dp[par] = ret;
}

signed main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  fact();
  cin >> N;
  vector<ll> A(N - 1, 0);
  vector<ll> B(N - 1, 0);
  for (ll i = 0; i < N - 1; ++i)
    cin >> A[i] >> B[i];
  for (ll i = 0; i < N - 1; ++i) {
    G[A[i]].emplace_back(B[i]);
    G[B[i]].emplace_back(A[i]);
  }
  for (ll i = 1; i <= N; ++i)
    prt(F(P(i, 0)))

        return 0;
}