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

#include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

LL dp[201][201];
LL dp2[201][201][201];
LL f(LL a, LL b, LL c) {
  LL &res = dp2[a][b][c];
  if (res >= 0)
    return res;

  if (a < 0 || b < 0 || c < 0 || a + b < c)
    return res = 0;
  if (a == 0)
    return res = (b == c ? 1 : 0);
  if (b == 0)
    return res = (a == c ? 1 : 0);
  if (c == 0)
    return res = (a + b == 0 ? 1 : 0);

  res =
      (f(a - 1, b, c - 1) + f(a, b - 1, c - 1) + f(a - 1, b - 1, c - 1)) % MOD;
  return res;
}

int sz[201];
void dfs(int u, VVI &G) {
  if (G[u].empty()) {
    sz[u] = 1;
    dp[u][1] = 1;
    return;
  }

  dfs(G[u][0], G);
  sz[u] = sz[G[u][0]];
  REP(i, sz[u] + 1)
  dp[u][i] = dp[G[u][0]][i];

  FOR(i, 1, SZ(G[u])) {
    int to = G[u][i];
    dfs(to, G);
    VL tmp(sz[u] + sz[to] + 1);
    FOR(c, 1, sz[u] + sz[to] + 1) {
      FOR(a, max(1, c - sz[to]), sz[u] + 1) {
        FOR(b, max(1, c - a), sz[to] + 1)
        (tmp[c] += dp[u][a] * dp[to][b] % MOD * f(a, b, c) % MOD) %= MOD;
      }
    }
    sz[u] += sz[to];
    REP(i, sz[u] + 1)
    dp[u][i] = tmp[i];
  }
  ++sz[u];
  for (int i = sz[u]; i > 0; --i)
    dp[u][i] = dp[u][i - 1];
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  fill((LL *)dp2, (LL *)dp2 + 201 * 201 * 201, -1);

  int N;
  cin >> N;
  VVI G(N);
  VI in(N);
  REP(i, N - 1) {
    int u, v;
    cin >> u >> v;
    G[u].PB(v);
    in[v]++;
  }
  int rt = -1;
  REP(i, N) if (in[i] == 0) rt = i;
  dfs(rt, G);
  LL ans = 0;
  REP(i, N + 1)(ans += dp[rt][i]) %= MOD;
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

LL dp[201][201];
LL dp2[201][201][201];
LL f(LL a, LL b, LL c) {
  if (a < 0 || b < 0 || c < 0 || a + b < c)
    return 0;
  if (a == 0)
    return b == c ? 1 : 0;
  if (b == 0)
    return a == c ? 1 : 0;
  if (c == 0)
    return a + b == 0 ? 1 : 0;
  LL &res = dp2[a][b][c];
  if (res)
    return res;

  (res += f(a - 1, b, c - 1) + f(a, b - 1, c - 1) + f(a - 1, b - 1, c - 1)) %=
      MOD;
  return res;
}

int sz[201];
void dfs(int u, VVI &G) {
  if (G[u].empty()) {
    sz[u] = 1;
    dp[u][1] = 1;
    return;
  }

  dfs(G[u][0], G);
  sz[u] = sz[G[u][0]];
  REP(i, sz[u] + 1)
  dp[u][i] = dp[G[u][0]][i];

  FOR(i, 1, SZ(G[u])) {
    int to = G[u][i];
    dfs(to, G);
    VL tmp(sz[u] + sz[to] + 1);
    FOR(a, 1, sz[u] + 1)
    FOR(b, 1, sz[to] + 1) for (int c = max(a, b); c <= a + b; ++c)(
        tmp[c] += dp[u][a] * dp[to][b] % MOD * f(a, b, c) % MOD) %= MOD;
    sz[u] += sz[to];
    REP(i, sz[u] + 1)
    dp[u][i] = tmp[i];
  }
  ++sz[u];
  for (int i = sz[u]; i > 0; --i)
    dp[u][i] = dp[u][i - 1];
  dp[u][0] = 0;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;
  VVI G(N);
  VI in(N);
  REP(i, N - 1) {
    int u, v;
    cin >> u >> v;
    G[u].PB(v);
    in[v]++;
  }
  int rt = -1;
  REP(i, N) if (in[i] == 0) rt = i;
  dfs(rt, G);
  LL ans = 0;
  REP(i, N + 1)(ans += dp[rt][i]) %= MOD;
  cout << ans << endl;

  return 0;
}