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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int MAX_N = 200005;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
} comb(MAX_N);

vector<int> to[MAX_N];

struct DP {
  mint dp;
  int t;
  DP(mint dp = 1, int t = 0) : dp(dp), t(t) {}
  DP &operator+=(const DP &a) {
    dp *= a.dp;
    dp *= comb(t + a.t, t);
    t += a.t;
    return *this;
  }
  DP operator-(const DP &a) const {
    DP res(*this);
    res.t -= a.t;
    res.dp /= comb(res.t + a.t, res.t);
    res.dp /= a.dp;
    return res;
  }
  DP addRoot() const {
    DP res(*this);
    res.t++;
    return res;
  }
};

vector<DP> dp[MAX_N];
DP dpSum[MAX_N];

DP dfs(int v, int p = -1) {
  // dpSum[v] = DP(1,0);
  dp[v] = vector<DP>(to[v].size());
  rep(i, to[v].size()) {
    int u = to[v][i];
    if (u == p)
      continue;
    dp[v][i] = dfs(u, v);
    dpSum[v] += dp[v][i];
  }
  return dpSum[v].addRoot();
}
void bfs(int v, const DP &dpP = DP(), int p = -1) {
  if (p != -1)
    dpSum[v] += dpP.addRoot();
  rep(i, to[v].size()) {
    int u = to[v][i];
    if (u == p)
      continue;
    bfs(u, dpSum[v] - dp[v][i], v);
  }
}

int main() {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  dfs(0);
  bfs(0);
  rep(i, n) { cout << dpSum[i].addRoot().dp << endl; }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
typedef long long ll;

struct Node {
  set<int> to;
};

vector<Node> nodes;
vector<bool> visit;

int dfs(int s, set<int> cand) {
  cand.erase(s);
  visit[s] = true;

  int res = 0;

  for (auto t : nodes[s].to)
    if (!visit[t])
      cand.insert(t);

  if (cand.empty()) {
    visit[s] = false;
    return 1;
  }

  for (auto c : cand)
    res += dfs(c, cand);

  visit[s] = false;
  return res;
}

int main() {
  int n;
  cin >> n;

  nodes.resize(n);

  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

    nodes[a].to.insert(b);
    nodes[b].to.insert(a);
  }

  for (int i = 0; i < n; i++) {
    visit.resize(n, false);
    set<int> cand;
    cand.insert(i);
    cout << dfs(i, cand) << endl;
  }
}