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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;

LL modpow(LL a, LL b) {
  LL r = 1;
  while (b) {
    if (b % 2 == 1)
      r = r * a % kMod;
    a = a * a % kMod;
    b /= 2;
  }
  return r;
}

LL invmod(LL a) { return modpow(a, kMod - 2); }

class Comb {
public:
  void Init(int N) {
    fact.resize(N + 1);
    inv.resize(N + 1);
    finv.resize(N + 1);
    fact[0] = fact[1] = 1LL;
    finv[0] = finv[1] = 1LL;
    inv[1] = 1;
    for (int i = 2; i <= N; i++) {
      fact[i] = fact[i - 1] * i % kMod;
      inv[i] = kMod - inv[kMod % i] * (kMod / i) % kMod;
      finv[i] = finv[i - 1] * inv[i] % kMod;
    }
  }

  LL nCr(int n, int r) const {
    LL numerator = fact[n];
    LL denominator = finv[r] * finv[n - r] % kMod;
    return (numerator * denominator) % kMod;
  }

private:
  vector<LL> fact;
  vector<LL> inv;
  vector<LL> finv;
};

struct Node {
  LL dp = 1;
  int size = 1;
};

LL N;
vector<vector<int>> G;
vector<Node> nodes;
Comb comb;

void dfs1(int v, int p = -1) {
  auto &node = nodes[v];
  for (int c : G[v]) {
    if (c == p)
      continue;
    dfs1(c, v);

    auto &child = nodes[c];
    node.size += child.size;
    node.dp = node.dp * child.dp % kMod;
    node.dp = node.dp * comb.nCr(node.size - 1, child.size) % kMod;
  }
}

void dfs2(int v, int p = -1) {
  auto &node = nodes[v];
  for (int c : G[v]) {
    if (c == p)
      continue;

    auto &child = nodes[c];
    int size = node.size - child.size;
    LL dp = node.dp * invmod(child.dp) % kMod;
    dp = dp * invmod(comb.nCr(node.size - 1, child.size)) % kMod;
    dp = dp * comb.nCr(size + child.size - 1, size) % kMod;

    child.size += size;
    child.dp = child.dp * dp % kMod;

    dfs2(c, v);
  }
}

int main() {
  cin >> N;
  G.resize(N);
  nodes.resize(N);
  comb.Init(N);

  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  dfs1(0);
  dfs2(0);
  for (int i = 0; i < N; ++i) {
    cout << nodes[i].dp % kMod << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;

LL modpow(LL a, LL b, LL mod) {
  LL r = 1;
  while (b) {
    if (b % 2 == 1)
      r = r * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return r;
}

LL invmod(LL a, LL mod) { return modpow(a, mod - 2, mod); }

vector<vector<int>> G;
vector<LL> parents;
vector<LL> dp, dp2;

void dfs1(int v) {
  LL num_nodes = 1;
  for (int n : G[v]) {
    if (n == parents[v])
      continue;
    parents[n] = v;
    dfs1(n);
    num_nodes += dp[n];
  }
  dp[v] = num_nodes;
}

void dfs2(int v, int child) {
  if (parents[v] > 0) {
    dfs2(parents[v], v);
  }
  dp2[v] -= dp[child];
  dp2[child] += dp2[v];

  dp2[v] %= kMod;
  dp2[child] %= kMod;
}

int main() {
  LL N;
  cin >> N;
  G.resize(N + 1);
  parents.resize(N + 1, -1);
  dp.resize(N + 1);

  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<LL> fact(N + 1);
  fact[0] = 1, fact[1] = 1;
  for (int i = 2; i <= N; ++i) {
    fact[i] = (fact[i - 1] * i) % kMod;
  }

  dfs1(1);
  LL total = fact[N];
  for (int i = 1; i <= N; ++i) {
    total = (total * invmod(dp[i], kMod)) % kMod;
  }
  cout << total << endl;

  for (int i = 2; i <= N; ++i) {
    dp2 = dp;
    dfs2(parents[i], i);
    LL total = fact[N];
    for (int i = 1; i <= N; ++i) {
      total = (total * invmod(dp2[i], kMod)) % kMod;
    }
    cout << total << endl;
  }
}