Problem p03175 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

class mint {
public:
  long long x;

  constexpr mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}

  constexpr mint operator-() const { return mint(-x); }

  constexpr mint &operator+=(const mint &rhs) {
    if ((x += rhs.x) >= MOD)
      x -= MOD;
    return *this;
  }

  constexpr mint &operator-=(const mint &rhs) {
    if ((x += MOD - rhs.x) >= MOD)
      x -= MOD;
    return *this;
  }

  constexpr mint &operator*=(const mint &rhs) {
    (x *= rhs.x) %= MOD;
    return *this;
  }

  constexpr mint operator+(const mint &rhs) const {
    mint res(*this);
    return res += rhs;
  }

  constexpr mint operator-(const mint &rhs) const {
    mint res(*this);
    return res -= rhs;
  }

  constexpr mint operator*(const mint &rhs) const {
    mint res(*this);
    return res *= rhs;
  }

  [[nodiscard]] constexpr mint pow(long long t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  [[nodiscard]] constexpr mint inv() const { return pow(MOD - 2); }

  mint &operator/=(mint rhs) { return *this *= rhs.inv(); }

  mint operator/(const mint rhs) const { return mint(*this) /= rhs; }

  constexpr bool operator==(const mint &rhs) const noexcept {
    return this->x == rhs.x;
  }

  constexpr bool operator!=(const mint &rhs) const noexcept {
    return this->x != rhs.x;
  }

  bool operator<(const mint &rhs) const noexcept { return this->x < rhs.x; }

  bool operator>(const mint &rhs) const noexcept { return this->x > rhs.x; }

  bool operator<=(const mint &rhs) const noexcept { return this->x <= rhs.x; }

  bool operator>=(const mint &rhs) const noexcept { return this->x >= rhs.x; }

  friend istream &operator>>(istream &is, mint &a) {
    long long t;
    is >> t;
    a = mint(t);
    return is;
  }

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

ll N;
Graph graph(N);
vector<vector<mint>> dp;
vector<vector<bool>> dp2;

mint dfs(ll u, ll prev, ll c) {
  if (dp2[u][c]) {
    return dp[u][c];
  }
  mint res = 1;
  for (ll v : graph[u]) {
    if (v != prev) {
      mint cnt = 0;
      if (c == 0) {
        cnt += dfs(v, u, 1);
        cnt += dfs(v, u, 0);
      } else {
        cnt += dfs(v, u, 0);
      }
      res *= cnt;
    }
  }
  return dp[u][c] = res;
}

void solve() {
  cin >> N;
  graph.resize(N);
  dp.resize(N, vector<mint>(2, 0));
  dp2.resize(N, vector<bool>(2, false));
  rep(i, N - 1) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  mint ans = dfs(0, -1, 0);
  ans += dfs(0, -1, 1);
  cout << ans << '\n';
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

class mint {
public:
  long long x;

  constexpr mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}

  constexpr mint operator-() const { return mint(-x); }

  constexpr mint &operator+=(const mint &rhs) {
    if ((x += rhs.x) >= MOD)
      x -= MOD;
    return *this;
  }

  constexpr mint &operator-=(const mint &rhs) {
    if ((x += MOD - rhs.x) >= MOD)
      x -= MOD;
    return *this;
  }

  constexpr mint &operator*=(const mint &rhs) {
    (x *= rhs.x) %= MOD;
    return *this;
  }

  constexpr mint operator+(const mint &rhs) const {
    mint res(*this);
    return res += rhs;
  }

  constexpr mint operator-(const mint &rhs) const {
    mint res(*this);
    return res -= rhs;
  }

  constexpr mint operator*(const mint &rhs) const {
    mint res(*this);
    return res *= rhs;
  }

  [[nodiscard]] constexpr mint pow(long long t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  [[nodiscard]] constexpr mint inv() const { return pow(MOD - 2); }

  mint &operator/=(mint rhs) { return *this *= rhs.inv(); }

  mint operator/(const mint rhs) const { return mint(*this) /= rhs; }

  constexpr bool operator==(const mint &rhs) const noexcept {
    return this->x == rhs.x;
  }

  constexpr bool operator!=(const mint &rhs) const noexcept {
    return this->x != rhs.x;
  }

  bool operator<(const mint &rhs) const noexcept { return this->x < rhs.x; }

  bool operator>(const mint &rhs) const noexcept { return this->x > rhs.x; }

  bool operator<=(const mint &rhs) const noexcept { return this->x <= rhs.x; }

  bool operator>=(const mint &rhs) const noexcept { return this->x >= rhs.x; }

  friend istream &operator>>(istream &is, mint &a) {
    long long t;
    is >> t;
    a = mint(t);
    return is;
  }

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

ll N;
Graph graph(N);
vector<vector<mint>> dp;
vector<vector<bool>> dp2;

mint dfs(ll u, ll prev, ll c) {
  if (dp2[u][c]) {
    return dp[u][c];
  }
  mint res = 1;
  for (ll v : graph[u]) {
    if (v != prev) {
      mint cnt = 0;
      if (c == 0) {
        cnt += dfs(v, u, 1);
        cnt += dfs(v, u, 0);
      } else {
        cnt += dfs(v, u, 0);
      }
      res *= cnt;
    }
  }
  dp2[u][c] = true;
  return dp[u][c] = res;
}

void solve() {
  cin >> N;
  graph.resize(N);
  dp.resize(N, vector<mint>(2, 0));
  dp2.resize(N, vector<bool>(2, false));
  rep(i, N - 1) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  mint ans = dfs(0, -1, 0);
  ans += dfs(0, -1, 1);
  cout << ans << '\n';
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}