Problem p03175 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define mem(x) memset(x, 0, sizeof(x));
#define what_is(x) cerr << #x << " is " << x << endl;
#define rep(i, a, b) for (int i = a; i < b; i++)

using namespace std;
const int N = 1e5;

vi ad[N];
bool visw[N], visb[N];
ll dpw[N], dpb[N], par[N];

ll dfsw(int x);

ll dfsb(int x) {
  if (visb[x])
    return dpb[x];
  visb[x] = true;
  dpb[x] = 1;

  for (auto y : ad[x]) {
    if (y == par[x])
      continue;
    par[y] = x;
    dpb[x] = (dpb[x] * dfsw(y)) % hell;
  }
  return dpb[x];
}

ll dfsw(int x) {
  if (visw[x] == true)
    return dpw[x];
  visw[x] = true;
  dpw[x] = 1;
  ll val1 = 1, val = 0;

  for (auto y : ad[x]) {

    if (y == par[x])
      continue;
    par[y] = x;
    val = 0;
    val = (val + dfsb(y)) % hell;
    val = (val + dfsw(y)) % hell;

    val1 = (val1 * val) % hell;
  }

  dpw[x] = val1;
  return dpw[x];
}

void solve() {

  int n;
  cin >> n;
  int u, v;
  rep(i, 0, n - 1) {
    cin >> u >> v;
    ad[u].pb(v);
    ad[v].pb(u);
  }

  par[1] = -1;
  dfsw(1);
  dfsb(1);

  cout << (dpb[1] + dpw[1]) % hell;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int t = 1, i = 1;
  // cin>>t;

  while (t--) {
    // cout<<"Case #"<<i++<<": ";
    solve();
  }

  return 0;
}

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

#define ll long long
#define ld long double
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define mem(x) memset(x, 0, sizeof(x));
#define what_is(x) cerr << #x << " is " << x << endl;
#define rep(i, a, b) for (int i = a; i < b; i++)

using namespace std;
const int N = 1e5 + 5;

vi ad[N];
bool visw[N], visb[N];
ll dpw[N], dpb[N], par[N];

ll dfsw(int x);

ll dfsb(int x) {
  if (visb[x])
    return dpb[x];
  visb[x] = true;
  dpb[x] = 1;

  for (auto y : ad[x]) {
    if (y == par[x])
      continue;
    par[y] = x;
    dpb[x] = (dpb[x] * dfsw(y)) % hell;
  }
  return dpb[x];
}

ll dfsw(int x) {
  if (visw[x] == true)
    return dpw[x];
  visw[x] = true;
  dpw[x] = 1;
  ll val1 = 1, val = 0;

  for (auto y : ad[x]) {

    if (y == par[x])
      continue;
    par[y] = x;
    val = 0;
    val = (val + dfsb(y)) % hell;
    val = (val + dfsw(y)) % hell;

    val1 = (val1 * val) % hell;
  }

  dpw[x] = val1;
  return dpw[x];
}

void solve() {

  int n;
  cin >> n;
  int u, v;
  rep(i, 0, n - 1) {
    cin >> u >> v;
    ad[u].pb(v);
    ad[v].pb(u);
  }

  par[1] = -1;
  dfsw(1);
  dfsb(1);

  cout << (dpb[1] + dpw[1]) % hell;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int t = 1, i = 1;
  // cin>>t;

  while (t--) {
    // cout<<"Case #"<<i++<<": ";
    solve();
  }

  return 0;
}