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

#include <bits/stdc++.h>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-9;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

int n, N = 0, M;
vvi g;
vi a, b;
vector<vvl> dp;

P dfs(int v, int d, int w) {
  P p = {d, v};
  for (auto u : g[v])
    if (u != w)
      p = max(p, dfs(u, d + 1, v));
  return p;
}

bool DFS(int v, int w, int p) {
  bool B = 0;
  if (v == w)
    B = 1;
  for (auto u : g[v])
    if (u != p) {
      B |= DFS(u, w, v);
    }
  if (B) {
    a.push_back(v);
    b[v]++;
    N++;
  }
  return B;
}

int Rec(int v, int p) {
  int D = 1, B = 0;
  vvl DP;
  for (auto u : g[v])
    if (u != p) {
      int d = Rec(u, v), D_ = D;
      D = max(D, d + 1);
      vvl DP_(D, vl(D));
      if (!B) {
        for (int i = 0; i < d; i++)
          for (int j = 0; j < d; j++) {
            (DP_[i + 1][j] += dp[u][i][j]) %= mod;
            (DP_[i][j + 1] += dp[u][i][j]) %= mod;
          }
        B = 1;
      } else {
        for (int i = 0; i < D_; i++)
          for (int j = 0; j < D_; j++)
            if (DP[i][j]) {
              for (int k = 0; k < min(d, M - j + 1); k++)
                for (int l = 0; l < min(d, M - i + 1); l++) {
                  if (j + k + 1 <= M)
                    (DP_[max(i, k + 1)][max(j, l)] += DP[i][j] * dp[u][k][l]) %=
                        mod;
                  if (i + l + 1 <= M)
                    (DP_[max(i, k)][max(j, l + 1)] += DP[i][j] * dp[u][k][l]) %=
                        mod;
                }
            }
      }
      DP = DP_;
    }
  for (auto u : g[v])
    if (u != p)
      dp[u].clear();
  if (D == 1) {
    DP = vvl(1, vl(1));
    DP[0][0]++;
  }
  dp[v] = DP;
  return D;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  b = vi(n);
  g = vvi(n);
  dp = vector<vvl>(n);
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  int L = dfs(0, 0, -1).second, R = dfs(L, 0, -1).second;
  DFS(L, R, -1);
  M = N / 2;
  ll C = a[N / 2], res = 0;
  Rec(C, -1);
  for (int i = 0; i <= M; i++)
    for (int j = 0; j <= M; j++)
      (res += dp[C][i][j]) %= mod;
  cout << res << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-9;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

int n, N = 0, M;
vvi g;
vi a, b;
vector<vvl> dp;

P dfs(int v, int d, int w) {
  P p = {d, v};
  for (auto u : g[v])
    if (u != w)
      p = max(p, dfs(u, d + 1, v));
  return p;
}

bool DFS(int v, int w, int p) {
  bool B = 0;
  if (v == w)
    B = 1;
  for (auto u : g[v])
    if (u != p) {
      B |= DFS(u, w, v);
    }
  if (B) {
    a.push_back(v);
    b[v]++;
    N++;
  }
  return B;
}

int Rec(int v, int p) {
  int D = 1, B = 0;
  vvl DP;
  for (auto u : g[v])
    if (u != p) {
      int d = Rec(u, v), D_ = D;
      D = max(D, d + 1);
      vvl DP_(D, vl(D));
      if (!B) {
        for (int i = 0; i < d; i++)
          for (int j = 0; j < d; j++) {
            (DP_[i + 1][j] += dp[u][i][j]) %= mod;
            (DP_[i][j + 1] += dp[u][i][j]) %= mod;
          }
        B = 1;
      } else {
        for (int i = 0; i < D_; i++)
          for (int j = 0; j < D_; j++)
            if (DP[i][j]) {
              for (int k = 0; k < min(d, M - j + 1); k++)
                for (int l = 0; l < min(d, M - i + 1); l++) {
                  if (j + k + 1 <= M)
                    (DP_[max(i, k + 1)][max(j, l)] += DP[i][j] * dp[u][k][l]) %=
                        mod;
                  if (i + l + 1 <= M)
                    (DP_[max(i, k)][max(j, l + 1)] += DP[i][j] * dp[u][k][l]) %=
                        mod;
                }
            }
      }
      DP = DP_;
    }
  if (D == 1) {
    DP = vvl(1, vl(1));
    DP[0][0]++;
  }
  dp[v] = DP;
  return D;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  b = vi(n);
  g = vvi(n);
  dp = vector<vvl>(n);
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  int L = dfs(0, 0, -1).second, R = dfs(L, 0, -1).second;
  DFS(L, R, -1);
  M = N / 2;
  ll C = a[N / 2], res = 0;
  Rec(C, -1);
  for (int i = 0; i <= M; i++)
    for (int j = 0; j <= M; j++)
      (res += dp[C][i][j]) %= mod;
  cout << res << endl;
}