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

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const long long mod = 1000000007;
long long dp2[500][500], r[500], dp[500][500];
vector<int> X[500];
int N, cnts;
inline long long ncr(long long n, long long r) {
  if (n - r < 0 || r < 0)
    return 0;
  return dp2[n - r][r];
}
inline long long solve(int p, int q) {
  if (dp[p][q] >= 0)
    return dp[p][q];
  if (X[p].size() == 0) {
    if (q == 1) {
      dp[p][q] = 1;
      return 1;
    }
    if (q != 1) {
      dp[p][q] = 0;
      return 0;
    }
  }
  if (q == 0)
    return 0;
  vector<vector<long long>> dp3(N + 1, vector<long long>(N + 1, 0));
  dp3[0][0] = 1;
  q--;
  int cnt1 = 0;
  for (int i = 0; i < X[p].size(); i++) {
    long long a3[210];
    for (int j = 0; j < N + 1; j++)
      a3[j] = 0;
    for (int j = 0; j < N; j++)
      a3[j] = solve(X[p][i], j);

    for (int j = 0; j <= cnt1; j++) {
      for (int k = 0; k <= r[X[p][i]] + 1; k++) {
        for (int l = 0; l <= k; l++) {
          int ko = j + l;
          if (ko > N)
            break;
          int res = j + k - ko;
          int LL = j - res;
          int RR = k - res;
          if (LL < 0 || RR < 0)
            continue;
          long long ans1 = dp3[i][j] * a3[k];
          ans1 %= mod;
          ans1 *= ncr(LL + RR, RR);
          ans1 %= mod;
          ans1 *= ncr(ko, res);
          ans1 %= mod;
          dp3[i + 1][ko] += ans1;
          if (dp3[i + 1][ko] >= mod)
            dp3[i + 1][ko] -= mod;
          cnts++;
        }
      }
    }
    cnt1 += r[X[p][i]] + 1;
  }
  long long ret = dp3[X[p].size()][q];
  q++;
  dp[p][q] = ret;
  for (int i = 1; i <= N; i++)
    dp[p][i] = dp3[X[p].size()][i - 1];
  return ret;
}
int main() {
  bool E[500];
  for (int i = 0; i < 500; i++) {
    E[i] = false;
    for (int j = 0; j < 500; j++) {
      dp[i][j] = -1;
      if (i == 0 || j == 0)
        dp2[i][j] = 1;
      else
        dp2[i][j] = (dp2[i - 1][j] + dp2[i][j - 1]) % mod;
    }
  }
  cin >> N;
  int TT = 0;
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    E[b] = true;
    X[a].push_back(b);
  }
  for (int i = 0; i < N; i++) {
    queue<int> Q;
    int cnt = 0;
    Q.push(i);
    while (!Q.empty()) {
      cnt++;
      int a1 = Q.front();
      Q.pop();
      for (int j = 0; j < X[a1].size(); j++) {
        Q.push(X[a1][j]);
      }
    }
    cnt--;
    r[i] = cnt;
  }
  for (int i = 0; i < N; i++) {
    if (E[i] == false)
      TT = i;
  }
  long long ans = 0;
  for (int i = 0; i <= N; i++) {
    ans += solve(TT, i);
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const long long mod = 1000000007;
long long dp2[500][500], r[500], dp[500][500];
vector<int> X[500];
int N, cnts;
inline long long ncr(long long n, long long r) {
  if (n - r < 0 || r < 0)
    return 0;
  return dp2[n - r][r];
}
inline long long solve(int p, int q) {
  if (dp[p][q] >= 0)
    return dp[p][q];
  if (X[p].size() == 0) {
    if (q == 1) {
      dp[p][q] = 1;
      return 1;
    }
    if (q != 1) {
      dp[p][q] = 0;
      return 0;
    }
  }
  if (q == 0)
    return 0;
  vector<vector<long long>> dp3(210, vector<long long>(210, 0));
  dp3[0][0] = 1;
  q--;
  for (int i = 0; i < X[p].size(); i++) {
    long long a3[210];
    for (int j = 0; j < 210; j++)
      a3[j] = 0;
    for (int j = 0; j < N; j++)
      a3[j] = solve(X[p][i], j);

    for (int j = 0; j <= N; j++) {
      for (int k = 0; k < N; k++) {
        for (int l = 0; l <= k; l++) {
          int ko = j + l;
          if (ko > N)
            break;
          int res = j + k - ko;
          int LL = j - res;
          int RR = k - res;
          if (LL < 0 || RR < 0)
            continue;
          long long ans1 = dp3[i][j] * a3[k];
          ans1 %= mod;
          ans1 *= ncr(LL + RR, RR);
          ans1 %= mod;
          ans1 *= ncr(ko, res);
          ans1 %= mod;
          dp3[i + 1][ko] += ans1;
          if (dp3[i + 1][ko] >= mod)
            dp3[i + 1][ko] -= mod;
          cnts++;
        }
      }
    }
  }
  long long ret = dp3[X[p].size()][q];
  q++;
  dp[p][q] = ret;
  for (int i = 1; i <= N; i++)
    dp[p][i] = dp3[X[p].size()][i - 1];
  return ret;
}
int main() {
  bool E[500];
  for (int i = 0; i < 500; i++) {
    E[i] = false;
    for (int j = 0; j < 500; j++) {
      dp[i][j] = -1;
      if (i == 0 || j == 0)
        dp2[i][j] = 1;
      else
        dp2[i][j] = (dp2[i - 1][j] + dp2[i][j - 1]) % mod;
    }
  }
  cin >> N;
  int TT = 0;
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    E[b] = true;
    X[a].push_back(b);
  }
  for (int i = 0; i < N; i++) {
    queue<int> Q;
    int cnt = 0;
    Q.push(i);
    while (!Q.empty()) {
      cnt++;
      int a1 = Q.front();
      Q.pop();
      for (int j = 0; j < X[a1].size(); j++) {
        Q.push(X[a1][j]);
      }
    }
    cnt--;
    r[i] = cnt;
  }
  for (int i = 0; i < N; i++) {
    if (E[i] == false)
      TT = i;
  }
  long long ans = 0;
  for (int i = 0; i <= N; i++) {
    ans += solve(TT, i);
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}