Problem p01723 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

typedef pair<int, int> Pi;
typedef tuple<int, int, int> Ti;
typedef vector<int> vint;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

int N;
vector<int> graph[222];
int indeg[222];

int dp[222][222];
int dp2[222][222][222];
int sz[222];

int dp3[2][222];

int dfs(int u) {
  sz[u] = 1;

  if (graph[u].empty()) {
    dp[u][1] = 1;
    return sz[u];
  }

  for (int v : graph[u])
    sz[u] += dfs(v);

  memset(dp3, 0, sizeof(dp3));
  int *curr = dp3[0];
  int *next = dp3[1];
  int ch = graph[u][0];
  rep(i, sz[ch] + 1) curr[i] = dp[ch][i] % mod;
  for (int v : graph[u]) {
    if (v == ch)
      continue;
    rep(j, 222) next[j] = 0;
    rep(j, sz[u]) rep(k, sz[u]) rep(l, sz[v] + 1) {
      (next[j] += curr[k] % mod * dp[v][l] % mod * dp2[k][l][j] % mod) %= mod;
    }
    swap(next, curr);
  }

  rep(i, sz[u]) dp[u][i + 1] = curr[i];

  return sz[u];
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  memset(dp2, 0, sizeof(dp2));
  dp2[0][0][0] = 1;
  rep(i, 201) rep(j, 201) rep(k, 201) {
    (dp2[i + 1][j][k + 1] += dp2[i][j][k]) %= mod;
    (dp2[i][j + 1][k + 1] += dp2[i][j][k]) %= mod;
    (dp2[i + 1][j + 1][k + 1] += dp2[i][j][k]) %= mod;
  }
  /*
  rep(i, 10) {
    rep(j, 10) cout << " " << dp2[0][i][j];
    cout << endl;
  }
  */
  memset(indeg, 0, sizeof(indeg));
  cin >> N;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    indeg[b]++;
  }

  int root = -1;
  rep(i, N) if (!indeg[i]) root = i;

  memset(dp, 0, sizeof(dp));
  memset(sz, 0, sizeof(sz));
  dfs(root);

  int ans = 0;
  rep(i, N + 1)(ans += dp[root][i]) %= mod;
  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

typedef pair<int, int> Pi;
typedef tuple<int, int, int> Ti;
typedef vector<int> vint;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

int N;
vector<int> graph[256];
int indeg[256];

int dp[256][256];
int dp2[256][256][256];
int sz[256];

int dp3[2][256];

int dfs(int u) {
  sz[u] = 1;

  if (graph[u].empty()) {
    dp[u][1] = 1;
    return sz[u];
  }

  for (int v : graph[u])
    sz[u] += dfs(v);

  memset(dp3, 0, sizeof(dp3));
  int *curr = dp3[0];
  int *next = dp3[1];
  int ch = graph[u][0];
  rep(i, sz[ch] + 1) curr[i] = dp[ch][i] % mod;
  for (int v : graph[u]) {
    if (v == ch)
      continue;
    rep(j, 256) next[j] = 0;
    rep(j, sz[u]) rep(k, sz[u]) rep(l, sz[v] + 1) {
      (next[j] += curr[k] % mod * dp[v][l] % mod * dp2[k][l][j] % mod) %= mod;
    }
    swap(next, curr);
  }

  rep(i, sz[u]) dp[u][i + 1] = curr[i];

  return sz[u];
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  memset(dp2, 0, sizeof(dp2));
  dp2[0][0][0] = 1;
  rep(i, 201) rep(j, 201) rep(k, 201) {
    (dp2[i + 1][j][k + 1] += dp2[i][j][k]) %= mod;
    (dp2[i][j + 1][k + 1] += dp2[i][j][k]) %= mod;
    (dp2[i + 1][j + 1][k + 1] += dp2[i][j][k]) %= mod;
  }
  /*
  rep(i, 10) {
    rep(j, 10) cout << " " << dp2[0][i][j];
    cout << endl;
  }
  */
  memset(indeg, 0, sizeof(indeg));
  cin >> N;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    indeg[b]++;
  }

  int root = -1;
  rep(i, N) if (!indeg[i]) root = i;

  memset(dp, 0, sizeof(dp));
  memset(sz, 0, sizeof(sz));
  dfs(root);

  int ans = 0;
  rep(i, N + 1)(ans += dp[root][i]) %= mod;
  cout << ans << endl;

  return 0;
}