Problem p03175 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <ext/numeric>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define oo 0x3f3f3f3f
#define OO 0x3f3f3f3f3f3f3f3f
#define ones(n) __builtin_popcount(n)
#define ONES(n) __builtin_popcountll(n)

using namespace std;
// using namespace __gnu_cxx;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef pair<int, int> ii;
typedef pair<long long, pair<int, int>> iii;
typedef pair<ii, ii> iiii;

const double PI = acos(-1.0), EPS = 1e-6;
const int N = 402, M = 100005, mod = 1000000007, mxLog = 20;
vector<int> adj[N];
int memo[N][2];

int solve(int u, int par, int par_color) {
  if (adj[u].size() == 0 || adj[u].size() == 1 && par != -1) {
    return 1 + par_color;
  }
  int &ret = memo[u][par_color];
  if (~ret)
    return ret;
  ret = 0;
  if (par_color) {
    long long x = 1;
    for (int v : adj[u]) {
      if (v == par)
        continue;
      x *= 1LL * solve(v, u, 1);
      x %= mod;
    }
    ret += x;
    x = 1;
    for (int v : adj[u]) {
      if (v == par)
        continue;
      x *= 1LL * solve(v, u, 0);
      x %= mod;
    }
    ret += x;
    ret %= mod;
  } else {
    long long x = 1;
    for (int v : adj[u]) {
      if (v == par)
        continue;
      x *= 1LL * solve(v, u, 1);
      x %= mod;
    }
    ret += x;
  }
  return ret;
}

int main() {
#ifndef ONLINE_JUDGE
//    freopen("input.txt", "rt", stdin);
//    freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10),
      cout << fixed;

  int n;
  cin >> n;
  int u, v;
  for (int i = 1; i < n; ++i) {
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  memset(memo, -1, sizeof memo);
  cout << solve(1, -1, 1) << '\n';

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
// #include <ext/numeric>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define oo 0x3f3f3f3f
#define OO 0x3f3f3f3f3f3f3f3f
#define ones(n) __builtin_popcount(n)
#define ONES(n) __builtin_popcountll(n)

using namespace std;
// using namespace __gnu_cxx;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef pair<int, int> ii;
typedef pair<long long, pair<int, int>> iii;
typedef pair<ii, ii> iiii;

const double PI = acos(-1.0), EPS = 1e-6;
const int N = 100005, M = 100005, mod = 1000000007, mxLog = 20;
vector<int> adj[N];
int memo[N][2];

int solve(int u, int par, int par_color) {
  if (adj[u].size() == 0 || adj[u].size() == 1 && par != -1) {
    return 1 + par_color;
  }
  int &ret = memo[u][par_color];
  if (~ret)
    return ret;
  ret = 0;
  if (par_color) {
    long long x = 1;
    for (int v : adj[u]) {
      if (v == par)
        continue;
      x *= 1LL * solve(v, u, 1);
      x %= mod;
    }
    ret += x;
    x = 1;
    for (int v : adj[u]) {
      if (v == par)
        continue;
      x *= 1LL * solve(v, u, 0);
      x %= mod;
    }
    ret += x;
    ret %= mod;
  } else {
    long long x = 1;
    for (int v : adj[u]) {
      if (v == par)
        continue;
      x *= 1LL * solve(v, u, 1);
      x %= mod;
    }
    ret += x;
  }
  return ret;
}

int main() {
#ifndef ONLINE_JUDGE
//    freopen("input.txt", "rt", stdin);
//    freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10),
      cout << fixed;

  int n;
  cin >> n;
  int u, v;
  for (int i = 1; i < n; ++i) {
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  memset(memo, -1, sizeof memo);
  cout << solve(1, -1, 1) << '\n';

  return 0;
}