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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = x; i < y; i++)
#define print(A, x, n)                                                         \\
  rep(i, 0, n) { cout << (i ? " " : "") << A[i] x; }                           \\
  cout << endl;
#define pprint(A, y, m, n)                                                     \\
  rep(j, 0, m) { print(A[j], y, n); }
const long mod = 1e9 + 7;
const int size = 2e5;
const int inf = 1e9;
vector<int> g[size];
vector<pair<int, long>> dp[size];
map<int, pair<int, long>> L[size], R[size];
pair<int, long> ans[size];
long fact[size];
void calc(int n) {
  fact[0] = 1;
  rep(i, 1, n + 1) { fact[i] = (fact[i - 1] * i) % mod; }
  return;
}
long modcomb(long n, long r) { // nCr mod p
  if (n < r)
    return 0;
  long x = (fact[n - r] * fact[r]) % mod;
  long res = 1;
  int i = 0;
  while ((mod - 2) >= (1 << i)) {
    if ((mod - 2) & (1 << i))
      (res *= x) %= mod;
    i++;
    x = x * x % mod;
  }
  return (res * fact[n]) % mod;
}
pair<int, long> func(pair<int, long> x, pair<int, long> y) {
  pair<int, long> z;
  z.first = x.first + y.first;
  z.second = x.second * y.second % mod;
  (z.second *= modcomb(z.first, x.first)) %= mod;
  return z;
}
pair<int, long> dfs1(int p, int v) {
  pair<int, int> res = {0, 1};
  for (int u : g[v]) {
    if (u == p)
      continue;
    dp[v].push_back(dfs1(v, u));
    res = func(res, dp[v].back());
  }
  res.first++;
  return res;
}
void dfs2(int p, int v) {
  ans[v] = func(R[v][0], dp[v].back());
  int j = 0;
  for (int u : g[v]) {
    if (u == p)
      continue;
    pair<int, long> t = func(dp[v].back(), func(L[v][j], R[v][j + 1]));
    t.first++;
    dp[u].push_back(t);
    dfs2(v, u);
    j++;
  }
  return;
}
int main() {
  int N;
  cin >> N;
  int a, b;
  rep(i, 0, N - 1) {
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  calc(size);
  dfs1(-1, 0);
  rep(i, 0, N) {
    L[i][0] = {0, 1};
    R[i][dp[i].size()] = {0, 1};
    rep(j, 0, dp[i].size()) L[i][j + 1] = func(L[i][j], dp[i][j]);
    for (int j = dp[i].size(); j > 0; j--)
      R[i][j - 1] = func(R[i][j], dp[i][j - 1]);
  }
  dp[0].push_back({0, 1});
  dfs2(-1, 0);
  rep(i, 0, N) cout << ans[i].second << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = x; i < y; i++)
#define print(A, x, n)                                                         \\
  rep(i, 0, n) { cout << (i ? " " : "") << A[i] x; }                           \\
  cout << endl;
#define pprint(A, y, m, n)                                                     \\
  rep(j, 0, m) { print(A[j], y, n); }
const long mod = 1e9 + 7;
const int size = 2e5;
const int inf = 1e9;
long ans[200000];
vector<int> g[200000];
map<int, map<int, pair<int, long>>> tree;
bool flag = false;
long fact[200001];
void calc(int n) {
  fact[0] = 1;
  rep(i, 1, n + 1) { fact[i] = (fact[i - 1] * i) % mod; }
  return;
}
long modcomb(long n, long r) { // nCr mod p
  if (n < r)
    return 0;
  long x = (fact[n - r] * fact[r]) % mod;
  long res = 1;
  int i = 0;
  while ((mod - 2) >= (1 << i)) {
    if ((mod - 2) & (1 << i))
      (res *= x) %= mod;
    i++;
    x = x * x % mod;
  }
  return (res * fact[n]) % mod;
}
pair<int, long> dfs(int p, int v) {
  if (tree[p][v].second)
    return tree[p][v];
  int cnt = 0;
  long res = 1;
  queue<pair<int, int>> vp;
  for (int u : g[v]) {
    if (u == p)
      continue;
    pair<int, long> t = dfs(v, u);
    cnt += t.first;
    vp.push(t);
  }
  int temp = cnt;
  while (!vp.empty()) {
    (res *= modcomb(temp, vp.front().first)) %= mod;
    (res *= vp.front().second) %= mod;
    temp -= vp.front().first;
    vp.pop();
  }
  tree[p][v] = {cnt + 1, res};
  return {cnt + 1, res};
}
void metadfs(int p, int v) {
  queue<pair<int, int>> vp;
  int cnt = 0;
  long res = 1;
  for (int u : g[v]) {
    if (u != p)
      metadfs(v, u);
    pair<int, long> t = dfs(v, u);
    cnt += t.first;
    vp.push(t);
  }
  int temp = cnt;
  while (!vp.empty()) {
    (res *= modcomb(temp, vp.front().first)) %= mod;
    (res *= vp.front().second) %= mod;
    temp -= vp.front().first;
    vp.pop();
  }
  ans[v] = res;
  return;
}
int main() {
  int N;
  cin >> N;
  int a, b;
  rep(i, 0, N - 1) {
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  calc(200001);
  metadfs(-1, 0);
  rep(i, 0, N) cout << ans[i] << endl;
}