Problem p03175 (C++)

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

#define int long long int
#define ld long double
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define endl "\n"

#define si set<int>
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector<pii>
#define mii map<int, int>
#define que_max priority_queue<int>
#define que_min priority_queue<int, vi, greater<int>>

#define loop(i, s, e) for (int i = s; i < e; i++)
#define rloop(i, e, s) for (int i = e; i >= s; i--)
#define mset(a, f) memset(a, f, sizeof(a))

#define M 1000000007
#define sz(x) ((int)x.size())
#define all(p) p.begin(), p.end()

#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
#define print(a)                                                               \
  for (auto x : a)                                                             \
    cout << x << " ";                                                          \
  cout << endl
#define Print(a, x, y)                                                         \
  for (int i = x; i < y; i++)                                                  \
    cout << a[i] << " ";                                                       \
  cout << endl

const int inf = INT_MAX;
const long long INF = LLONG_MAX;

inline int power(int a, int b) {
  int x = 1;
  while (b) {
    if (b & 1)
      x *= a;
    a *= a;
    b >>= 1;
  }
  return x;
}

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

const int N = 3e5 + 7;
vector<vi> adj(N);

bool vis[N];
// 0-->white
int dp[100005][2];
int n;
/* In tree vale questions no need to keep the visited array as each node can be
visited only once as each node has exactly one parent except the root node which
has no parent. while doing dfs of a tree we only need to keep track of the
parent node.*/
int dfs(int id, int p, int pc) {

  int &ans = dp[id][pc];
  if (ans != -1)
    return ans;

  int a1 = 1;
  for (auto i : adj[id]) {
    if (i != p) {

      a1 *= dfs(i, id, 0);
      a1 %= M;
    }
  }
  int a2 = 0;

  if (pc != 1) {
    a2 = 1;
    for (auto i : adj[id]) {
      if (i != p) {
        a2 *= dfs(i, id, 1);
        a2 %= M;
      }
    }
  }

  return ans = (a1 + a2) % M;
}
void solve() {
  cin >> n;
  adj.resize(n + 1);
  mset(dp, -1);
  loop(i, 0, n - 1) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }

  cout << dfs(1, 0, 3) << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cout << setprecision(9) << fixed;

  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int long long int
#define ld long double
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define endl "\n"

#define si set<int>
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector<pii>
#define mii map<int, int>
#define que_max priority_queue<int>
#define que_min priority_queue<int, vi, greater<int>>

#define loop(i, s, e) for (int i = s; i < e; i++)
#define rloop(i, e, s) for (int i = e; i >= s; i--)
#define mset(a, f) memset(a, f, sizeof(a))

#define M 1000000007
#define sz(x) ((int)x.size())
#define all(p) p.begin(), p.end()

#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
#define print(a)                                                               \
  for (auto x : a)                                                             \
    cout << x << " ";                                                          \
  cout << endl
#define Print(a, x, y)                                                         \
  for (int i = x; i < y; i++)                                                  \
    cout << a[i] << " ";                                                       \
  cout << endl

const int inf = INT_MAX;
const long long INF = LLONG_MAX;

inline int power(int a, int b) {
  int x = 1;
  while (b) {
    if (b & 1)
      x *= a;
    a *= a;
    b >>= 1;
  }
  return x;
}

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

const int N = 3e5 + 7;
vector<vi> adj(N);

bool vis[N];
// 0-->white
int dp[100005][2];
int n;
/* In tree vale questions no need to keep the visited array as each node can be
visited only once as each node has exactly one parent except the root node which
has no parent. while doing dfs of a tree we only need to keep track of the
parent node.*/
int dfs(int id, int p, int pc) {

  int &ans = dp[id][pc];
  if (ans != -1)
    return ans;

  int a1 = 1;
  for (auto i : adj[id]) {
    if (i != p) {

      a1 *= dfs(i, id, 0);
      a1 %= M;
    }
  }
  int a2 = 0;

  if (pc != 1) {
    a2 = 1;
    for (auto i : adj[id]) {
      if (i != p) {
        a2 *= dfs(i, id, 1);
        a2 %= M;
      }
    }
  }

  return ans = (a1 + a2) % M;
}
void solve() {
  cin >> n;
  adj.resize(n + 1);
  mset(dp, -1);
  loop(i, 0, n - 1) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }

  cout << dfs(1, 0, 3) << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cout << setprecision(9) << fixed;

  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}