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

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

ll mpow(ll x,
        ll n) { // x^n(mod mo) ←普通にpow(x,n)では溢れてしまうため，随時mod計算
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % mo;
    x = x * x % mo;
    n = n >> 1;
  }
  return ans;
}

const int mod = 1000000007;
class mint {
  long long x;

public:
  mint(long long 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 {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint &a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint &a) const {
    mint res(*this);
    return res *= 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 {
    mint res(*this);
    return res /= a;
  }

  friend ostream &operator<<(ostream &os, const mint &m) {
    os << m.x;
    return os;
  }
};

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(200010);

vector<vector<ll>> v(200005);

struct DP {
  mint dp;
  ll t;
  DP(mint dp = 1, ll t = 1) : dp(dp), t(t){};
  DP &operator+=(const DP &a) {
    dp *= a.dp;
    // dp %= mo;
    dp *= comb(t + a.t - 1, a.t);
    // dp %= mo;
    t += a.t;
    return *this;
  }
  DP &operator-=(const DP &a) {
    t -= a.t;
    dp /= comb(t + a.t - 1, a.t);
    // dp %= mo;
    dp /= a.dp;
    // dp %= mo;
    return *this;
  }
};

DP memo[200010];
DP ans[200010];

DP dfs(ll u, ll p = -1) {
  DP dp;
  // cout << u << endl;
  for (auto &e : v[u]) {
    if (e == p)
      continue;
    // dp += dfs(e,u);
    dp += dfs(e, u);
    // memo[u] += memo[e];
  }
  memo[u] = dp;
  return dp;
}
void bfs(ll u, ll p = -1) {
  for (auto &e : v[u]) {
    if (e == p)
      continue;
    DP d = memo[u];
    d -= memo[e];
    memo[e] += d;
    bfs(e, u);
  }
}

int main() {
  ll N;
  cin >> N;
  ll a, b;
  rep(i, N - 1) {
    cin >> a >> b;
    v[--a].push_back(--b);
    v[b].push_back(a);
  }
  // cout << "a"<<endl;
  // ll ans = dfs(0).dp;
  dfs(0);
  bfs(0);
  rep(i, N) { cout << memo[i].dp << endl; }
  /*
  stack<pair<ll,ll>> st;
  vector<ll> dist(N,1e10);
  st.push(make_pair(i,0));
  while(!st.empty()){
      ll u = st.top().first;
      ll d = st.top().second;
      st.pop();
      if(dist[u] != 1e10)continue;
      // cout <<"huhuh"<<endl;
      dist[u] = d;
      memo[i][u] = d;
      rep(j,v[u].size()){
          que.push(make_pair(v[u][j], d+1));
      }
  }
  cout << sum << endl;
  */
}


Error: TLE

Fixed submission:

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

ll mpow(ll x,
        ll n) { // x^n(mod mo) ←普通にpow(x,n)では溢れてしまうため，随時mod計算
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % mo;
    x = x * x % mo;
    n = n >> 1;
  }
  return ans;
}

ll combi(ll n, ll r) {
  if (r * 2 > n)
    r = n - r;
  ll dividend = 1;
  ll divisor = 1;
  for (ll i = 1; i <= r; ++i) {
    dividend *= (n - i + 1);
    dividend %= mo;
    divisor *= i;
    divisor %= mo;
  }
  return (dividend * mpow(divisor, mo - 2)) % mo;
}

vector<vector<ll>> v(200005);

struct DP {
  ll dp;
  ll t;
  DP(ll dp = 1, ll t = 1) : dp(dp), t(t){};
  DP &operator+=(const DP &a) {
    dp *= a.dp;
    dp %= mo;
    dp *= combi(t + a.t - 1, a.t);
    dp %= mo;
    t += a.t;
    return *this;
  }
  DP &operator-=(const DP &a) {
    t -= a.t;
    dp *= mpow(combi(t + a.t - 1, a.t), mo - 2);
    dp %= mo;
    dp *= mpow(a.dp, mo - 2);
    dp %= mo;
    return *this;
  }
};

DP memo[200010];
DP ans[200010];

DP dfs(ll u, ll p = -1) {
  DP dp;
  // cout << u << endl;
  for (auto &e : v[u]) {
    if (e == p)
      continue;
    // dp += dfs(e,u);
    dp += dfs(e, u);
    // memo[u] += memo[e];
  }
  memo[u] = dp;
  return dp;
}
void bfs(ll u, ll p = -1) {
  for (auto &e : v[u]) {
    if (e == p)
      continue;
    DP d = memo[u];
    d -= memo[e];
    memo[e] += d;
    bfs(e, u);
  }
}

int main() {
  ll N;
  cin >> N;
  ll a, b;
  rep(i, N - 1) {
    cin >> a >> b;
    v[--a].push_back(--b);
    v[b].push_back(a);
  }
  // cout << "a"<<endl;
  // ll ans = dfs(0).dp;
  dfs(0);
  bfs(0);
  rep(i, N) { cout << memo[i].dp << endl; }
  /*
  stack<pair<ll,ll>> st;
  vector<ll> dist(N,1e10);
  st.push(make_pair(i,0));
  while(!st.empty()){
      ll u = st.top().first;
      ll d = st.top().second;
      st.pop();
      if(dist[u] != 1e10)continue;
      // cout <<"huhuh"<<endl;
      dist[u] = d;
      memo[i][u] = d;
      rep(j,v[u].size()){
          que.push(make_pair(v[u][j], d+1));
      }
  }
  cout << sum << endl;
  */
}