Problem p03175 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define MOD 1000000007
#define fi(i, a, b) for (ll i = a; i < b; i++)
#define fd(i, a, b) for (ll i = a; i >= b; i--)
#define cfi(container, it)                                                     \
  for (auto it = container.begin(); it != container.end(); it++)
#define cfd(container, it)                                                     \
  for (auto it = container.rbegin(); it != container.rend(); it++)
#define vi vector<int>
#define vll vector<ll>
#define pb push_back
#define pbx(num)                                                               \
  ll temp;                                                                     \
  cin >> temp;                                                                 \
  num.pb(temp)

using namespace std;
typedef long long ll;

struct modll {
  ll num;
  operator ll() { return num; }
};
modll operator*(modll x, modll y) { return {(x.num * y.num) % MOD}; }
modll operator+(modll x, modll y) { return {(x.num + y.num + MOD) % MOD}; }

int n;
const int nax = 1e5 + 5;
// first for white and second for black
vector<pair<ll, ll>> dp(nax, {1, 1});
vi adj[nax];
vector<bool> vis(nax);

void add_self(ll &a, ll b) {
  a += b;
  if (a > MOD)
    a -= MOD;
}

void mul_self(ll &a, ll b) {
  a *= b;
  while (a > MOD)
    a -= MOD;
}

void dfs(int i) {

  vis[i] = true;

  cfi(adj[i], it) {
    if (!vis[*it]) {
      dfs(*it);
      ll temp = 0;
      add_self(temp, dp[*it].first);
      add_self(temp, dp[*it].second);
      mul_self(dp[i].first, temp);
      mul_self(dp[i].second, dp[*it].first);
      // flag = true;
    }
  }

  // if(!flag) {
  // 	dp[i].first = dp[i].second = 1;
  // }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  fi(i, 0, n - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    adj[x].pb(y);
    adj[y].pb(x);
  }

  ll ans = 0;

  // because its a tree and everything is connected
  dfs(0);
  add_self(ans, dp[0].first);
  add_self(ans, dp[0].second);

  cout << ans << '\n';

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007
#define fi(i, a, b) for (ll i = a; i < b; i++)
#define fd(i, a, b) for (ll i = a; i >= b; i--)
#define cfi(container, it)                                                     \
  for (auto it = container.begin(); it != container.end(); it++)
#define cfd(container, it)                                                     \
  for (auto it = container.rbegin(); it != container.rend(); it++)
#define vi vector<int>
#define vll vector<ll>
#define pb push_back
#define pbx(num)                                                               \
  ll temp;                                                                     \
  cin >> temp;                                                                 \
  num.pb(temp)

using namespace std;
typedef long long ll;

struct modll {
  ll num;
  operator ll() { return num; }
};
modll operator*(modll x, modll y) { return {(x.num * y.num) % MOD}; }
modll operator+(modll x, modll y) { return {(x.num + y.num + MOD) % MOD}; }

int n;
const int nax = 1e5 + 5;
// first for white and second for black
vector<pair<ll, ll>> dp(nax, {1, 1});
vi adj[nax];
vector<bool> vis(nax);

void add_self(ll &a, ll b) {
  a += b;
  if (a > MOD)
    a -= MOD;
}

void mul_self(ll &a, ll b) {
  a *= b;
  a %= MOD;
  if (a < 0)
    a += MOD;
}

void dfs(int i) {

  vis[i] = true;

  cfi(adj[i], it) {
    if (!vis[*it]) {
      dfs(*it);
      ll temp = 0;
      add_self(temp, dp[*it].first);
      add_self(temp, dp[*it].second);
      mul_self(dp[i].first, temp);
      mul_self(dp[i].second, dp[*it].first);
      // flag = true;
    }
  }

  // if(!flag) {
  // 	dp[i].first = dp[i].second = 1;
  // }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  fi(i, 0, n - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    adj[x].pb(y);
    adj[y].pb(x);
  }

  ll ans = 0;

  // because its a tree and everything is connected
  dfs(0);
  add_self(ans, dp[0].first);
  add_self(ans, dp[0].second);

  cout << ans << '\n';

  return 0;
}