Problem p03278 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> P;

const int L = 5001;
const LL M = 1e9 + 7;
vector<int> dp[L][2];
vector<int> es[L];

LL g[L];
vector<LL> buf[2];
void f(int r, int p, vector<LL> u[2]) {
  u[0].push_back(0);
  u[0].push_back(1);
  u[1].push_back(0);
  u[1].push_back(0);
  for (auto c : es[r]) {
    if (c == p)
      continue;
    vector<LL> v[2];
    f(c, r, v);
    for (int i = 0; i < 2; i++) {
      buf[i].clear();
      buf[i].resize(u[i].size() + v[i].size());
    }
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < 2; j++) {
        for (int k = 1; k < u[i].size(); k++) {
          for (int l = 1; l < v[j].size(); l++) {
            (buf[(i + j) % 2][k + l] += u[i][k] * v[j][l] % M) %= M;
            (buf[(i + j + 1) % 2][k] += u[i][k] * v[j][l] % M * g[l] % M) %= M;
          }
        }
      }
    }
    for (int i = 0; i < 2; i++) {
      buf[i].swap(u[i]);
    }
  }
}
int main() {
  int N;
  cin >> N;
  for (int i = 1; i < N; i++) {
    int x, y;
    cin >> x >> y;
    es[x].push_back(y);
    es[y].push_back(x);
  }
  g[0] = 1;
  for (int i = 2; i <= N; i += 2) {
    g[i] = g[i - 2] * (i - 1) % M;
  }
  vector<LL> u[2];
  f(1, 0, u);
  LL ret = 0;
  for (int i = 1; i < u[0].size(); i++) {
    (ret += u[0][i] * g[i] % M) %= M;
  }
  for (int i = 1; i < u[1].size(); i++) {
    (ret += M - u[1][i] * g[i] % M) %= M;
  }
  cout << ret << endl;
  // cout << f(es[s][0].first,s,0,1) << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> P;

const int L = 5001;
const LL M = 1e9 + 7;
vector<int> dp[L][2];
vector<int> es[L];

LL g[L];
vector<LL> buf[2];
void f(int r, int p, vector<LL> u[2]) {
  u[0].push_back(0);
  u[0].push_back(1);
  u[1].push_back(0);
  u[1].push_back(0);
  for (auto c : es[r]) {
    if (c == p)
      continue;
    vector<LL> v[2];
    f(c, r, v);
    for (int i = 0; i < 2; i++) {
      buf[i].clear();
      buf[i].resize(u[i].size() + v[i].size() - 1);
    }
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < 2; j++) {
        for (int k = 1; k < u[i].size(); k++) {
          for (int l = 1; l < v[j].size(); l++) {
            (buf[(i + j) % 2][k + l] += u[i][k] * v[j][l] % M) %= M;
            (buf[(i + j + 1) % 2][k] += u[i][k] * v[j][l] % M * g[l] % M) %= M;
          }
        }
      }
    }
    for (int i = 0; i < 2; i++) {
      buf[i].swap(u[i]);
    }
  }
}
int main() {
  int N;
  cin >> N;
  for (int i = 1; i < N; i++) {
    int x, y;
    cin >> x >> y;
    es[x].push_back(y);
    es[y].push_back(x);
  }
  g[0] = 1;
  for (int i = 2; i <= N; i += 2) {
    g[i] = g[i - 2] * (i - 1) % M;
  }
  vector<LL> u[2];
  f(1, 0, u);
  LL ret = 0;
  for (int i = 1; i < u[0].size(); i++) {
    (ret += u[0][i] * g[i] % M) %= M;
  }
  for (int i = 1; i < u[1].size(); i++) {
    (ret += M - u[1][i] * g[i] % M) %= M;
  }
  cout << ret << endl;
  // cout << f(es[s][0].first,s,0,1) << endl;

  return 0;
}