Problem p03278 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;

int sum(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; }
int sub(int a, int b) { return a - b < 0 ? a - b + MOD : a - b; }
int mul(int a, int b) { return (1LL * a * b) % MOD; }

const int MAXN = 5007;
int df[MAXN];

vector<int> edg[MAXN];
int sbtr[MAXN];

int dp[MAXN][MAXN][2];

void dfs(int u, int bap) {
  sbtr[u] = 1;

  dp[u][1][0] = 1;
  dp[u][1][1] = 0;
  for (int v : edg[u]) {
    if (v == bap)
      continue;
    dfs(v, u);

    int s0 = 0;
    int s1 = 0;
    for (int niche = 2; niche <= sbtr[v]; niche += 2) {
      s0 = sum(s0, mul(dp[v][niche][0], df[niche]));
      s1 = sum(s1, mul(dp[v][niche][1], df[niche]));
    }

    for (int total = sbtr[u] + sbtr[v]; total > 0; total--) {
      int _0 = dp[u][total][0];
      int _1 = dp[u][total][1];

      for (int p = 0; p < 2; p++) {
        int &ans = dp[u][total][p];
        ans = 0;
        for (int niche = 1; niche <= sbtr[v] && niche < total; niche++) {
          int bam = total - niche;
          ans = sum(ans, mul(dp[u][bam][0], dp[v][niche][p]));
          ans = sum(ans, mul(dp[u][bam][1], dp[v][niche][p ^ 1]));
        }

        if (total <= sbtr[u]) {
          if (p == 1) {
            ans = sum(ans, mul(_0, s0));
            ans = sum(ans, mul(_1, s1));
          } else {
            ans = sum(ans, mul(_0, s1));
            ans = sum(ans, mul(_1, s0));
          }
        }
      }
    }
    sbtr[u] += sbtr[v];
  }

  //    for (int total = 1; total <= sbtr[u]; total++) {
  //        for (int p = 0; p < 2; p++) {
  //            cout << "dp[" << u << "][" << total << "][" << p << "] = " <<
  //            dp[u][total][p] << endl;
  //        }
  //    }
  //    cout << endl;
}

int main() {
  int n;
  cin >> n;

  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    edg[u].push_back(v);
    edg[v].push_back(u);
  }

  df[0] = 1;
  for (int i = 2; i <= n; i += 2) {
    df[i] = mul(i - 1, df[i - 2]);
    //        cout << "df[" << i << "] = " << df[i] << endl;
  }

  dfs(1, 0);

  int ans = 0;
  for (int sz = 2; sz <= n; sz++) {
    ans = sum(ans, mul(sub(dp[1][sz][0], dp[1][sz][1]), df[sz]));
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;

int sum(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; }
int sub(int a, int b) { return a - b < 0 ? a - b + MOD : a - b; }
int mul(int a, int b) { return (1LL * a * b) % MOD; }

const int MAXN = 5007;
int df[MAXN];

vector<int> edg[MAXN];
int sbtr[MAXN];

int dp[MAXN][MAXN][2];

void dfs(int u, int bap) {
  sbtr[u] = 1;

  dp[u][1][0] = 1;
  dp[u][1][1] = 0;
  for (int v : edg[u]) {
    if (v == bap)
      continue;
    dfs(v, u);

    int s0 = 0;
    int s1 = 0;
    for (int niche = 2; niche <= sbtr[v]; niche += 2) {
      s0 = sum(s0, mul(dp[v][niche][0], df[niche]));
      s1 = sum(s1, mul(dp[v][niche][1], df[niche]));
    }

    for (int total = sbtr[u] + sbtr[v]; total > 0; total--) {
      int _0 = dp[u][total][0];
      int _1 = dp[u][total][1];

      for (int p = 0; p < 2; p++) {
        int &ans = dp[u][total][p];
        ans = 0;
        for (int niche = max(total - sbtr[u], 1);
             niche <= sbtr[v] && niche < total; niche++) {
          int bam = total - niche;
          ans = sum(ans, mul(dp[u][bam][0], dp[v][niche][p]));
          ans = sum(ans, mul(dp[u][bam][1], dp[v][niche][p ^ 1]));
        }

        if (total <= sbtr[u]) {
          if (p == 1) {
            ans = sum(ans, mul(_0, s0));
            ans = sum(ans, mul(_1, s1));
          } else {
            ans = sum(ans, mul(_0, s1));
            ans = sum(ans, mul(_1, s0));
          }
        }
      }
    }
    sbtr[u] += sbtr[v];
  }

  //    for (int total = 1; total <= sbtr[u]; total++) {
  //        for (int p = 0; p < 2; p++) {
  //            cout << "dp[" << u << "][" << total << "][" << p << "] = " <<
  //            dp[u][total][p] << endl;
  //        }
  //    }
  //    cout << endl;
}

int main() {
  int n;
  cin >> n;

  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    edg[u].push_back(v);
    edg[v].push_back(u);
  }

  df[0] = 1;
  for (int i = 2; i <= n; i += 2) {
    df[i] = mul(i - 1, df[i - 2]);
    //        cout << "df[" << i << "] = " << df[i] << endl;
  }

  dfs(1, 0);

  int ans = 0;
  for (int sz = 2; sz <= n; sz++) {
    ans = sum(ans, mul(sub(dp[1][sz][0], dp[1][sz][1]), df[sz]));
  }

  cout << ans << endl;

  return 0;
}