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

#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
pair<long long, int> P = make_pair(1, 0);
vector<vector<int>> child(vector<vector<int>> &E, int r) {
  int N = E.size();
  vector<vector<int>> c(N);
  queue<int> Q;
  Q.push(r);
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    for (int w : E[v]) {
      if (c[w].empty()) {
        c[v].push_back(w);
        Q.push(w);
      }
    }
  }
  return c;
}
long long modpow(long long a, long long b) {
  a %= MOD;
  long long res = 1;
  while (b > 0) {
    if (b % 2 == 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b = b / 2;
  }
  return res;
}
long long modinv(long long a) { return modpow(a, MOD - 2); }
vector<long long> mf;
long long modfact(long long n) {
  if (n < mf.size()) {
    return mf[n];
  } else {
    if (mf.empty())
      mf.push_back(1);
    long long res = mf.back();
    for (int i = mf.size(); i <= n; i++) {
      res = res * i % MOD;
      mf.push_back(res);
    }
    return res;
  }
}
long long modbinom1(long long n, long long r) {
  long long res;
  res = modfact(n);
  res = res * modinv(modfact(r)) % MOD;
  res = res * modinv(modfact(n - r)) % MOD;
  return res;
}
pair<long long, int> add(pair<long long, int> A, pair<long long, int> B) {
  return make_pair((A.first * B.first) % MOD *
                       modbinom1(A.second + B.second, B.second) % MOD,
                   A.second + B.second);
}
pair<long long, int> root(pair<long long, int> A) {
  return make_pair(A.first, A.second + 1);
}
pair<long long, int> dfs1(vector<pair<long long, int>> &dp,
                          vector<vector<int>> &c, int v) {
  for (int w : c[v])
    dp[v] = add(dp[v], root(dfs1(dp, c, w)));
  return dp[v];
}
void dfs2(vector<pair<long long, int>> &dp1, vector<pair<long long, int>> &dp2,
          vector<vector<int>> &c, int v) {
  int deg = c[v].size();
  vector<pair<long long, int>> L(deg + 1, P), R(deg + 1, P);
  for (int i = 0; i < deg; i++)
    L[i + 1] = add(L[i], root(dp1[c[v][i]]));
  for (int i = deg - 1; i >= 0; i--)
    R[i] = add(R[i + 1], root(dp1[c[v][i]]));
  for (int i = 0; i < deg; i++) {
    dp2[c[v][i]] = root(add(dp2[v], add(L[i], R[i + 1])));
    dfs2(dp1, dp2, c, c[v][i]);
  }
}
int main() {
  int N;
  cin >> N;
  vector<vector<int>> E(N);
  for (int i = 0; i < N - 1; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    E[x].push_back(y);
    E[y].push_back(x);
  }
  vector<vector<int>> c = child(E, 0);
  vector<pair<long long, int>> dp1(N, P), dp2(N, P);
  dfs1(dp1, c, 0);
  dfs2(dp1, dp2, c, 0);
  for (int i = 0; i < N; i++) {
    cout << add(dp1[i], dp2[i]).first << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
vector<long long> mf;
long long modfact(long long n) {
  if (n < mf.size()) {
    return mf[n];
  } else {
    if (mf.empty())
      mf.push_back(1);
    long long res = mf.back();
    for (int i = mf.size(); i <= n; i++) {
      res = res * i % MOD;
      mf.push_back(res);
    }
    return res;
  }
}
long long modpow(long long a, long long b) {
  a %= MOD;
  long long res = 1;
  while (b > 0) {
    if (b % 2 == 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b = b / 2;
  }
  return res;
}
long long modinv(long long a) { return modpow(a, MOD - 2); }
int dfs1(vector<int> &dp1, vector<vector<int>> &c, int start) {
  if (dp1[start] != -1) {
    return dp1[start];
  }
  int ans = 0;
  for (int i = 0; i < c[start].size(); i++) {
    ans += dfs1(dp1, c, c[start][i]);
  }
  ans++;
  dp1[start] = ans;
  return ans;
}
long long dfs2(vector<int> &dp1, vector<long long> &dp2, vector<vector<int>> &c,
               int start) {
  if (dp2[start] != -1) {
    return dp2[start];
  }
  long long ans = modfact(dp1[start] - 1);
  for (int i = 0; i < c[start].size(); i++) {
    ans *= modinv(modfact(dp1[c[start][i]]));
    ans %= MOD;
    ans *= dfs2(dp1, dp2, c, c[start][i]);
    ans %= MOD;
  }
  dp2[start] = ans;
  return ans;
}
long long dfs4(vector<int> &dp1, vector<long long> &dp2, vector<long long> &dp4,
               int N, vector<int> &p, vector<vector<int>> &c, int start) {
  if (dp4[start] != -1) {
    return dp4[start];
  }
  long long ans = modfact(N - dp1[start] - 1);
  if (p[start] != 0) {
    ans *= modinv(modfact(N - dp1[p[start]]));
    ans %= MOD;
    ans *= dfs4(dp1, dp2, dp4, N, p, c, p[start]);
    ans %= MOD;
  }
  for (int i = 0; i < c[p[start]].size(); i++) {
    int v = c[p[start]][i];
    if (v != start) {
      ans *= modinv(modfact(dp1[v]));
      ans %= MOD;
      ans *= dp2[v];
      ans %= MOD;
    }
  }
  dp4[start] = ans;
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  vector<vector<int>> E(N);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    E[a].push_back(b);
    E[b].push_back(a);
  }
  vector<int> p(N, -1);
  queue<int> Q;
  Q.push(0);
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    for (int i = 0; i < E[v].size(); i++) {
      if (p[E[v][i]] == -1) {
        p[E[v][i]] = v;
        Q.push(E[v][i]);
      }
    }
  }
  vector<vector<int>> c(N);
  for (int i = 1; i < N; i++) {
    c[p[i]].push_back(i);
  }
  /*
  for (int i = 0; i < N; i++){
    cout << p[i] << ' ';
  }
  cout << endl;
  */
  vector<int> dp1(N, -1); // vertex count, to root
  dfs1(dp1, c, 0);
  /*
  for (int i = 0; i < N; i++){
    cout << dp1[i] << ' ';
  }
  cout << endl;
  */
  vector<long long> dp2(N, -1); // count, to root
  dfs2(dp1, dp2, c, 0);
  /*
  for (int i = 0; i < N; i++){
    cout << dp2[i] << ' ';
  }
  cout << endl;
  */
  vector<long long> dp4(N, -1); // count, from root
  for (int i = 1; i < N; i++) {
    dfs4(dp1, dp2, dp4, N, p, c, i);
  }
  /*
  for (int i = 0; i < N; i++){
    cout << dp4[i] << ' ';
  }
  cout << endl;
  */
  vector<long long> Ans(N);
  Ans[0] = dp2[0];
  for (int i = 1; i < N; i++) {
    long long ans;
    // cout << "i=" << i << endl;
    ans = modfact(N - 1);
    // if (i == 3) cout << "ans1 = " << ans << endl;
    ans *= modinv(modfact(N - dp1[i]));
    ans %= MOD;
    // if (i == 3) cout << "ans2 = " << ans << endl;
    ans *= dp4[i];
    ans %= MOD;
    // if (i == 3) cout << "ans3 = " << ans << endl;
    for (int j = 0; j < c[i].size(); j++) {
      ans *= modinv(modfact(dp1[c[i][j]]));
      ans %= MOD;
      ans *= dp2[c[i][j]];
      ans %= MOD;
      // if (i == 3) cout << "ans4 = " << ans << endl;
    }
    // if (i == 3) cout << "ans5 = " << ans << endl;
    Ans[i] = ans;
    // cout << "i=" << i << ", ans[i] =" << Ans[i] << endl;
  }
  for (int i = 0; i < N; i++) {
    cout << Ans[i] << endl;
  }
}