Problem p03175 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <fstream>
#include <iostream>
#include <vector>

#define NMAX 100005
#define MOD 1000000007

#define f cin
using namespace std;

// ifstream f("date.in");
vector<long long int> a[NMAX];
vector<long long int> b[NMAX];
long long int n;
long long int dp[NMAX][2];
long long int putere_doi[NMAX];
long long int vis[NMAX];

void solve(long long int x) {
  vis[x] = 1;
  for (int i = 0; i < a[x].size(); ++i) {
    int vecin = a[x][i];
    if (vis[vecin] == 0) {
      b[x].push_back(vecin);
      solve(vecin);
    }
  }
}

long long int adauga_suma(vector<long long int> vect, long long int &s,
                          long long int current_index) {
  if (current_index == vect.size())
    return -1;
  if (current_index == vect.size() - 1) {
    long long int vecin = vect[current_index];
    // s = (s + ((dp[vecin][0] + dp[vecin][1]) % MOD)) % MOD;
    return ((dp[vecin][0] + dp[vecin][1]) % MOD);
  }
  long long int curr_ans = adauga_suma(vect, s, current_index + 1);
  long long int vecin = vect[current_index];
  // s = (s + ((dp[vecin][0] + dp[vecin][1]) * curr_ans) % MOD ) % MOD;
  return (((dp[vecin][0] + dp[vecin][1]) * curr_ans) % MOD);
}

void calc_sum(long long int x) {
  /// 0 -> alb
  /// 1 -> negru
  for (long long int i = 0; i < b[x].size(); ++i) {
    long long int vecin = b[x][i];
    calc_sum(vecin);
  }
  if (b[x].size() == 0) {
    dp[x][0] = 1;
    dp[x][1] = 1;
  } else {
    long long int sum = 1;
    for (long long int i = 0; i < b[x].size(); ++i) {
      int vecin = b[x][i];
      sum = (sum * dp[vecin][0]) % MOD;
    }
    dp[x][1] = sum;
    long long int sum_negru = 0;
    long long int nr = b[x].size();
    sum_negru = adauga_suma(b[x], sum_negru, 0) % MOD;
    dp[x][0] = sum_negru;
  }
}

int main() {

  f >> n;
  for (long long int i = 0; i < n - 1; ++i) {
    long long int x, y;
    f >> x >> y;
    x--;
    y--;
    a[x].push_back(y);
    a[y].push_back(x);
  }

  solve(0);
  calc_sum(0);

  long long int ans = (dp[0][0] + dp[0][1]) % MOD;

  /*for (int i = 0; i < n; ++i) {
      cout << dp[i][0] << " " << dp[i][1] << "\n";
  }*/

  cout << ans;
  return 0;
}


Fixed submission:
#include <fstream>
#include <iostream>
#include <vector>

#define NMAX 100005
#define MOD 1000000007

#define f cin
using namespace std;

// ifstream f("date.in");
vector<long long int> a[NMAX];
vector<long long int> b[NMAX];
long long int n;
long long int dp[NMAX][2];
long long int putere_doi[NMAX];
long long int vis[NMAX];

void solve(long long int x) {
  vis[x] = 1;
  for (int i = 0; i < a[x].size(); ++i) {
    int vecin = a[x][i];
    if (vis[vecin] == 0) {
      b[x].push_back(vecin);
      solve(vecin);
    }
  }
}

long long int adauga_suma(vector<long long int> vect, long long int &s,
                          long long int current_index) {
  if (current_index == vect.size())
    return -1;
  if (current_index == vect.size() - 1) {
    long long int vecin = vect[current_index];
    // s = (s + ((dp[vecin][0] + dp[vecin][1]) % MOD)) % MOD;
    return ((dp[vecin][0] + dp[vecin][1]) % MOD);
  }
  long long int curr_ans = adauga_suma(vect, s, current_index + 1);
  long long int vecin = vect[current_index];
  // s = (s + ((dp[vecin][0] + dp[vecin][1]) * curr_ans) % MOD ) % MOD;
  return (((dp[vecin][0] + dp[vecin][1]) * curr_ans) % MOD);
}

void calc_sum(long long int x) {
  /// 0 -> alb
  /// 1 -> negru
  for (long long int i = 0; i < b[x].size(); ++i) {
    long long int vecin = b[x][i];
    calc_sum(vecin);
  }
  if (b[x].size() == 0) {
    dp[x][0] = 1;
    dp[x][1] = 1;
  } else {
    long long int sum = 1;
    for (long long int i = 0; i < b[x].size(); ++i) {
      int vecin = b[x][i];
      sum = (sum * dp[vecin][0]) % MOD;
    }
    dp[x][1] = sum;
    long long int sum_negru = 0;
    long long int nr = b[x].size();
    long long int curr_ans = 1;
    for (int i = b[x].size() - 1; i >= 0; --i) {
      int vecin = b[x][i];
      curr_ans = (curr_ans * (dp[vecin][0] + dp[vecin][1]) % MOD) % MOD;
    }
    sum_negru = curr_ans;
    dp[x][0] = sum_negru;
  }
}

int main() {

  f >> n;
  for (long long int i = 0; i < n - 1; ++i) {
    long long int x, y;
    f >> x >> y;
    x--;
    y--;
    a[x].push_back(y);
    a[y].push_back(x);
  }

  solve(0);
  calc_sum(0);

  long long int ans = (dp[0][0] + dp[0][1]) % MOD;

  /*for (int i = 0; i < n; ++i) {
      cout << dp[i][0] << " " << dp[i][1] << "\n";
  }*/

  cout << ans;
  return 0;
}