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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
const int inf = 1e9 + 7;
const ll INF = 1e18;
int mod = 1000000007;
#define abs(x) (x >= 0 ? x : -(x))
#define lb(v, x) (int)(lower_bound(all(v), x) - v.begin())
#define ub(v, x) (int)(upper_bound(all(v), x) - v.begin())
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <typename T> T pow(T a, int b) {
  return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1;
}
ll modpow(ll a, ll b, ll _mod) {
  return b ? modpow(a * a % _mod, b / 2, _mod) * (b % 2 ? a : 1) % _mod : 1;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (auto &vi : vec)
    os << vi << " ";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << p.F << " " << p.S;
  return os;
}
template <typename T> inline istream &operator>>(istream &is, vector<T> &v) {
  rep(j, sz(v)) is >> v[j];
  return is;
}
template <class T> inline void add(T &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}

void solve();

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);

  int T;
  // cin >> T;
  T = 1;

  while (T--) {
    solve();
  }
}

const int MAX = 200005;
ll fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int 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;
  }
}
// 二項係数計算
ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

int n;
vector<vector<int>> G(MAX);
// その頂点と部分木の塗り方
vector<int> c(MAX);
vector<ll> d(MAX); // debuged, ll

void dfs(int v, int p) {
  d[v] = 1;
  vector<int> cs;
  for (int nv : G[v]) {
    if (nv == p)
      continue;
    dfs(nv, v);
    (d[v] *= d[nv]) %= mod;
    cs.eb(c[nv] + 1);
  }
  int sum = accumulate(all(cs), 0);
  c[v] = sum;
  for (int c : cs) {
    (d[v] *= COM(sum, c)) %= mod;
    sum -= c;
  }
}

vector<ll> ans(MAX);
void dfs2(int v, int C, ll D, int p) {
  ans[v] = 1;

  vector<int> cs;
  cs.eb(C);
  for (int nv : G[v]) {
    if (nv == p)
      continue;
    cs.eb(c[nv] + 1);
  }
  int sum = accumulate(all(cs), 0);
  for (int c : cs) {
    (ans[v] *= COM(sum, c)) %= mod;
    sum -= c;
  }

  (ans[v] *= D) %= mod;
  for (int nv : G[v]) {
    if (nv == p)
      continue;
    (ans[v] *= d[nv]) %= mod;
  }

  // calc next D
  for (int nv : G[v]) {
    if (nv == p)
      continue;
    (D *= d[nv]) %= mod;
  }
  sum = accumulate(all(cs), 0);
  for (int c : cs) {
    (D *= COM(sum, c)) %= mod;
    sum -= c;
  }
  sum = accumulate(all(cs), 0);
  for (int nv : G[v]) {
    if (nv == p)
      continue;
    ll D2 = D;
    (D2 *= modpow(COM(sum, c[nv] + 1), mod - 2, mod)) %= mod;
    (D2 *= modpow(d[nv], mod - 2, mod)) %= mod;
    dfs2(nv, C + 1 + c[v] - (c[nv] + 1), D2, v);
  }
}
void solve() {
  cin >> n;

  COMinit();

  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].eb(b);
    G[b].eb(a);
  }
  dfs(0, -1);
  // rep(i, n) cout << d[i] << " "; cout << endl;
  dfs2(0, 0, 1, -1);
  rep(i, n) cout << ans[i] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
const int inf = 1e9 + 7;
const ll INF = 1e18;
int mod = 1000000007;
#define abs(x) (x >= 0 ? x : -(x))
#define lb(v, x) (int)(lower_bound(all(v), x) - v.begin())
#define ub(v, x) (int)(upper_bound(all(v), x) - v.begin())
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <typename T> T pow(T a, int b) {
  return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1;
}
ll modpow(ll a, ll b, ll _mod) {
  return b ? modpow(a * a % _mod, b / 2, _mod) * (b % 2 ? a : 1) % _mod : 1;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (auto &vi : vec)
    os << vi << " ";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << p.F << " " << p.S;
  return os;
}
template <typename T> inline istream &operator>>(istream &is, vector<T> &v) {
  rep(j, sz(v)) is >> v[j];
  return is;
}
template <class T> inline void add(T &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}

void solve();

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);

  int T;
  // cin >> T;
  T = 1;

  while (T--) {
    solve();
  }
}

const int MAX = 200005;
ll fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int 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;
  }
}
// 二項係数計算
ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

int n;
vector<vector<int>> G(MAX);
// その頂点と部分木の塗り方
vector<int> c(MAX);
vector<ll> d(MAX); // debuged, ll

void dfs(int v, int p) {
  d[v] = 1;
  vector<int> cs;
  for (int nv : G[v]) {
    if (nv == p)
      continue;
    dfs(nv, v);
    (d[v] *= d[nv]) %= mod;
    cs.eb(c[nv] + 1);
  }
  int sum = accumulate(all(cs), 0);
  c[v] = sum;
  for (int c : cs) {
    (d[v] *= COM(sum, c)) %= mod;
    sum -= c;
  }
}

vector<ll> ans(MAX);
void dfs2(int v, int C, ll D, int p) {
  ans[v] = 1;

  vector<int> cs;
  cs.eb(C);
  for (int nv : G[v]) {
    if (nv == p)
      continue;
    cs.eb(c[nv] + 1);
  }
  int sum = accumulate(all(cs), 0);
  for (int c : cs) {
    (ans[v] *= COM(sum, c)) %= mod;
    sum -= c;
  }

  (ans[v] *= D) %= mod;
  for (int nv : G[v]) {
    if (nv == p)
      continue;
    (ans[v] *= d[nv]) %= mod;
  }

  // calc next D
  for (int nv : G[v]) {
    if (nv == p)
      continue;
    (D *= d[nv]) %= mod;
  }
  // cout << v << " " << D << endl;
  for (int nv : G[v]) {
    if (nv == p)
      continue;
    vector<int> cs2;
    cs2.eb(C);
    for (int nv2 : G[v]) {
      if (nv2 == nv || nv2 == p)
        continue;
      cs2.eb(c[nv2] + 1);
    }
    ll D2 = D;
    sum = accumulate(all(cs2), 0);
    for (int c : cs2) {
      (D2 *= COM(sum, c)) %= mod;
      sum -= c;
    }
    (D2 *= modpow(d[nv], mod - 2, mod)) %= mod;
    dfs2(nv, C + 1 + c[v] - (c[nv] + 1), D2, v);
  }
}
void solve() {
  cin >> n;

  COMinit();

  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].eb(b);
    G[b].eb(a);
  }
  dfs(0, -1);
  // rep(i, n) cout << d[i] << " "; cout << endl;
  dfs2(0, 0, 1, -1);
  rep(i, n) cout << ans[i] << endl;
}