Problem p03175 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define popcnt(a) __builtin_popcount(a)
#define FastIO() ios::sync_with_stdio(false), cin.tie(0);
#define IO() freopen("palindrome.in", "rw", stdin)
#define error(args...)                                                         \
  {                                                                            \
    cerr << "LINE " << __LINE__;                                               \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
    cerr << endl;                                                              \
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << ' ' << *it << " = " << a;
  err(++it, args...);
}
typedef long long LL;
const int N = 1e5 + 9, M = 3e6 + 9, OO = 0x3f3f3f3f, MOD = 1e9 + 7;
int n;
vector<int> adj[N];
LL memo[N][2];
LL solve(int v, int c, int p) {

  LL &ret = memo[v][c];
  error(ret, v, c, p);
  if (~ret)
    return ret;
  ret = 1;

  for (auto ch : adj[v]) {
    if (ch != p) {
      if (c == 0)
        ret = (ret * (solve(ch, 0, v) + solve(ch, 1, v))) % MOD;
      else
        ret = (ret * (solve(ch, 0, v))) % MOD;
    }
  }

  return ret % MOD;
}

int main() {
  memset(memo, -1, sizeof memo);
  scanf("%d", &n);
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  printf("%lld\n", (solve(1, 0, -1) + solve(1, 1, -1)) % MOD);
#ifdef _LOCAL_DEFINE
  cerr << (double)clock() * 1.0 / CLOCKS_PER_SEC << endl;
#endif
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define popcnt(a) __builtin_popcount(a)
#define FastIO() ios::sync_with_stdio(false), cin.tie(0);
#define IO() freopen("palindrome.in", "rw", stdin)
#define error(args...)                                                         \
  {                                                                            \
    cerr << "LINE " << __LINE__;                                               \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
    cerr << endl;                                                              \
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << ' ' << *it << " = " << a;
  err(++it, args...);
}
typedef long long LL;
const int N = 1e5 + 9, M = 3e6 + 9, OO = 0x3f3f3f3f, MOD = 1e9 + 7;
int n;
vector<int> adj[N];
LL memo[N][2];
LL solve(int v, int c, int p) {

  LL &ret = memo[v][c];
  if (~ret)
    return ret;
  ret = 1;

  for (auto ch : adj[v]) {
    if (ch != p) {
      if (c == 0)
        ret = (ret * (solve(ch, 0, v) + solve(ch, 1, v))) % MOD;
      else
        ret = (ret * (solve(ch, 0, v))) % MOD;
    }
  }

  return ret % MOD;
}

int main() {
  memset(memo, -1, sizeof memo);
  scanf("%d", &n);
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  printf("%lld\n", (solve(1, 0, -1) + solve(1, 1, -1)) % MOD);
#ifdef _LOCAL_DEFINE
  cerr << (double)clock() * 1.0 / CLOCKS_PER_SEC << endl;
#endif
  return 0;
}