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

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl "\\n"
#define ll long long
#define oo 0x3f3f3f3fLL
#define sz(s) (int)(s.size())
#define RT(s) return cout << s, 0
#define INF 0x3f3f3f3f3f3f3f3fLL
#define all(v) v.begin(), v.end()
#define watch(x) cout << (#x) << " = " << x << endl
const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1};
const int dc[]{0, 1, 1, 1, 0, -1, -1, -1};
#if __cplusplus >= 201402L
template <typename T> vector<T> create(size_t n) { return vector<T>(n); }
template <typename T, typename... Args> auto create(size_t n, Args... args) {
  return vector<decltype(create<T>(args...))>(n, create<T>(args...));
}
#endif
void run() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef EZZAT
  freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
#else
#endif
}

const int N = 2e5 + 9, mod = 1e9 + 7;
vector<int> adj[N];
int size[N];
ll mem[N], mem2[N];
ll fact[N];
ll inv[N];     // mod inverse for i
ll invfact[N]; // mod inverse for i!
void factInverse() {
  fact[0] = inv[1] = fact[1] = invfact[0] = invfact[1] = 1;
  for (long long i = 2; i < N; i++) {
    fact[i] = (fact[i - 1] * i) % mod;
    inv[i] = mod - (inv[mod % i] * (mod / i) % mod);
    invfact[i] = (inv[i] * invfact[i - 1]) % mod;
  }
}

template <typename T> ll mul(T a) { return a; }
template <typename T, typename... Args> ll mul(T a, Args... b) {
  return (a * mul(b...)) % mod;
}

ll power(int x, int y = mod) {
  if (y == 0)
    return 1;
  if (y == 1)
    return x;
  ll r = power(x, y >> 1);
  return mul(r, r, power(x, y & 1));
}
ll nCr(int n, int r) {
  if (r > n)
    return 0;
  return mul(fact[n], invfact[n - r], invfact[r]);
}

ll dfs(int node, int par) {
  ll ans = 1;
  for (int ch : adj[node]) {
    if (ch == par)
      continue;
    ll tmp = dfs(ch, node);
    size[node] += size[ch];
    ans = mul(ans, tmp, nCr(size[node], size[ch]));
  }
  mem[node] = ans;
  size[node]++;
  return ans;
}

int n;
void dfs2(int node, int par) {
  if (~par) {
    ll tmp = mem2[par];
    ll myAns = mul(mem[node], nCr(n - 1, size[node]));
    tmp = mul(tmp, power(myAns, mod - 2));
    int sz = n - size[node];
    mem2[node] = mul(mem[node], tmp, nCr(n - 1, sz));
  } else
    mem2[node] = mem[node];
  for (int ch : adj[node]) {
    if (ch == par)
      continue;
    dfs2(ch, node);
  }
}
int main() {
  run();
  factInverse();
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1, -1);
  dfs2(1, -1);
  for (int i = 1; i <= n; i++)
    cout << mem2[i] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl "\\n"
#define ll long long
#define oo 0x3f3f3f3fLL
#define sz(s) (int)(s.size())
#define RT(s) return cout << s, 0
#define INF 0x3f3f3f3f3f3f3f3fLL
#define all(v) v.begin(), v.end()
#define watch(x) cout << (#x) << " = " << x << endl
const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1};
const int dc[]{0, 1, 1, 1, 0, -1, -1, -1};
#if __cplusplus >= 201402L
template <typename T> vector<T> create(size_t n) { return vector<T>(n); }
template <typename T, typename... Args> auto create(size_t n, Args... args) {
  return vector<decltype(create<T>(args...))>(n, create<T>(args...));
}
#endif
void run() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef EZZAT
  freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
#else
#endif
}

const int N = 2e5 + 9, mod = 1e9 + 7;
vector<int> adj[N];
int size[N];

ll fact[N];
ll inv[N];     // mod inverse for i
ll invfact[N]; // mod inverse for i!
void factInverse() {
  fact[0] = inv[1] = fact[1] = invfact[0] = invfact[1] = 1;
  for (long long i = 2; i < N; i++) {
    fact[i] = (fact[i - 1] * i) % mod;
    inv[i] = mod - (inv[mod % i] * (mod / i) % mod);
    invfact[i] = (inv[i] * invfact[i - 1]) % mod;
  }
}

template <typename T> ll mul(T a) { return a; }
template <typename T, typename... Args> ll mul(T a, Args... b) {
  return (a * mul(b...)) % mod;
}

ll nCr(int n, int r) { return mul(fact[n], invfact[n - r], invfact[r]); }

map<int, pair<ll, int>> mem[N];
pair<ll, int> dfs(int node, int par) {
  if (mem[node].count(par))
    return mem[node][par];
  ll ans = 1;
  int sz = 0;
  for (int ch : adj[node]) {
    if (ch == par)
      continue;
    pair<ll, int> p = dfs(ch, node);
    sz += p.second;
    ans = mul(ans, p.first, nCr(sz, p.second));
  }
  return mem[node][par] = {ans, sz + 1};
}

int main() {
  run();
  factInverse();
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; i++)
    cout << dfs(i, i).first << endl;
}