Problem p03278 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD =
    1000000000 + 7; // ;//1000000000 + 7 998244353 924844033 1000000000 + 9;
constexpr long long INF = numeric_limits<LL>::max();
constexpr long long DINF = 1000000000000;
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<double, double> Dll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
struct Fll {
  LL first, second, third, fourth;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Rrep(i, mf) for (LL i = mf - 1; i >= 0; i--)
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
struct Edge {
  LL to, cost;
};
struct edge {
  LL from, to, cost;
};
vector<vector<LL>> g;
vector<edge> ed;
vector<Pll> pv;
map<string, LL> ma;
set<LL> st;
int di[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
LL n, m, k, p, q, w, h, ans, cnt, sum, sub[5100], fact[5100], a[210000],
    b[210000];
vector<LL> dp[2][5100];
string str;
bool f;
char c[1100][1100];
void dfs(LL cur, LL par) {
  sub[cur]++;
  dp[0][cur].push_back(0);
  dp[1][cur].push_back(0);
  dp[0][cur].push_back(1);
  dp[1][cur].push_back(0);
  rep(x, g[cur].size()) {
    LL v = g[cur][x];
    if (v == par)
      continue;
    dfs(v, cur);
    vector<LL> table[2];
    table[0].resize(sub[cur] + sub[v] + 1, 0);
    table[1].resize(sub[cur] + sub[v] + 1, 0);
    for (LL i = 1; i <= sub[cur]; i++) {
      for (LL j = 1; j <= sub[v]; j++) {
        table[0][i + j] += (dp[0][v][j] * dp[0][cur][i] % MOD +
                            dp[1][v][j] * dp[1][cur][i] % MOD) %
                           MOD;
        table[1][i + j] += (dp[0][v][j] * dp[1][cur][i] % MOD +
                            dp[1][v][j] * dp[0][cur][i] % MOD) %
                           MOD;
        table[0][i] +=
            ((((dp[0][v][j] * fact[j]) % MOD) * dp[1][cur][i]) % MOD +
             (((dp[1][v][j] * fact[j]) % MOD) * dp[0][cur][i]) % MOD) %
            MOD;
        table[1][i] +=
            ((((dp[0][v][j] * fact[j]) % MOD) * dp[0][cur][i]) % MOD +
             (((dp[1][v][j] * fact[j]) % MOD) * dp[1][cur][i]) % MOD) %
            MOD;
        table[0][i + j] %= MOD;
        table[1][i + j] %= MOD;
        table[0][i] %= MOD;
        table[1][i] %= MOD;
      }
    }
    swap(table[0], dp[0][cur]);
    swap(table[1], dp[1][cur]);
    sub[cur] += sub[v];
  }
}
int main() {
  cin >> n;
  g.resize(n);
  fact[2] = 1;
  for (int i = 2; i <= n + 1; i++) {
    if (i % 2 == 0)
      fact[i + 2] = (fact[i] * (i + 1)) % MOD;
  }
  rep(i, n - 1) {
    LL x, y;
    cin >> x >> y;
    x--, y--;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  dfs(0, -1);
  for (LL i = 1; i <= n; i++) {
    ans += ((dp[0][0][i] - dp[1][0][i] + MOD) % MOD) * fact[i] % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD =
    1000000000 + 7; // ;//1000000000 + 7 998244353 924844033 1000000000 + 9;
constexpr long long INF = numeric_limits<LL>::max();
constexpr long long DINF = 1000000000000;
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<double, double> Dll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
struct Fll {
  LL first, second, third, fourth;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Rrep(i, mf) for (LL i = mf - 1; i >= 0; i--)
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
struct Edge {
  LL to, cost;
};
struct edge {
  LL from, to, cost;
};
vector<vector<LL>> g;
vector<edge> ed;
vector<Pll> pv;
map<string, LL> ma;
set<LL> st;
int di[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
LL n, m, k, p, q, w, h, ans, cnt, sum, sub[5100], fact[5100], a[210000],
    b[210000];
vector<LL> dp[2][5100];
string str;
bool f;
char c[1100][1100];
void dfs(LL cur, LL par) {
  sub[cur]++;
  dp[0][cur].push_back(0);
  dp[1][cur].push_back(0);
  dp[0][cur].push_back(1);
  dp[1][cur].push_back(0);
  rep(x, g[cur].size()) {
    LL v = g[cur][x];
    if (v == par)
      continue;
    dfs(v, cur);
    vector<LL> table[2];
    table[0].resize(sub[cur] + sub[v] + 1, 0);
    table[1].resize(sub[cur] + sub[v] + 1, 0);
    for (LL i = 1; i <= sub[cur]; i++) {
      for (LL j = 1; j <= sub[v]; j++) {
        table[0][i + j] +=
            (dp[0][v][j] * dp[0][cur][i] + dp[1][v][j] * dp[1][cur][i]) % MOD;
        table[1][i + j] +=
            (dp[0][v][j] * dp[1][cur][i] + dp[1][v][j] * dp[0][cur][i]) % MOD;
        table[0][i] += ((((dp[0][v][j] * fact[j]) % MOD) * dp[1][cur][i]) +
                        (((dp[1][v][j] * fact[j]) % MOD) * dp[0][cur][i])) %
                       MOD;
        table[1][i] += ((((dp[0][v][j] * fact[j]) % MOD) * dp[0][cur][i]) +
                        (((dp[1][v][j] * fact[j]) % MOD) * dp[1][cur][i])) %
                       MOD;
        table[0][i + j] %= MOD;
        table[1][i + j] %= MOD;
        table[0][i] %= MOD;
        table[1][i] %= MOD;
      }
    }
    swap(table[0], dp[0][cur]);
    swap(table[1], dp[1][cur]);
    sub[cur] += sub[v];
  }
}
int main() {
  cin >> n;
  g.resize(n);
  fact[2] = 1;
  for (int i = 2; i <= n + 1; i++) {
    if (i % 2 == 0)
      fact[i + 2] = (fact[i] * (i + 1)) % MOD;
  }
  rep(i, n - 1) {
    LL x, y;
    cin >> x >> y;
    x--, y--;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  dfs(0, -1);
  for (LL i = 1; i <= n; i++) {
    ans += ((dp[0][0][i] - dp[1][0][i] + MOD) % MOD) * fact[i] % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}