Problem p01723 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n;
vector<int> G[201];
vector<int> rG[201];
ll fie[205][205][205];
ll dp[205][205];
ll nxt[205];
int pa;
int siz[205];

void solve(int v, int p) {
  int ns = 0;
  dp[v][0] = 1;
  for (int i = 0; i < G[v].size(); i++) {
    int nv = G[v][i];
    if (nv != p) {
      solve(nv, v);
      memset(nxt, 0, sizeof(nxt));
      for (int j = 0; j <= ns; j++) {
        for (int k = 0; k <= siz[nv]; k++) {
          for (int l = 0; l <= min(j, k); l++) {
            ll val = dp[v][j] * dp[nv][k] % MOD;
            val = (val * fie[j][k][l]) % MOD;
            // printf("%d %d %d %lld %lld %lld\\n",j,k,l,dp[v][j],dp[nv][k],val);
            nxt[j + k - l] += val;
            nxt[j + k - l] %= MOD;
          }
        }
      }
      ns += siz[nv];
      for (int j = 0; j <= ns; j++) {
        dp[v][j] = nxt[j];
      }
    }
  }
  for (int i = ns; i >= 0; i--) {
    dp[v][i + 1] = dp[v][i];
  }
  dp[v][0] = 0;
}

void dfs(int v, int p) {
  pa = v;
  for (int i = 0; i < rG[v].size(); i++) {
    int nv = rG[v][i];
    if (nv != p) {
      dfs(rG[v][i], v);
    }
  }
}
int dfs2(int v, int p) {
  int ans = 1;
  for (int i = 0; i < G[v].size(); i++) {
    int nv = G[v][i];
    if (nv != p) {
      ans += dfs2(G[v][i], v);
    }
  }
  return (siz[v] = ans);
}

void init() {
  fie[0][0][0] = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        fie[i + 1][j][k] += fie[i][j][k];
        fie[i + 1][j][k] %= MOD;
        fie[i + 1][j + 1][k + 1] += fie[i][j][k];
        fie[i + 1][j + 1][k + 1] %= MOD;
        fie[i][j + 1][k] += fie[i][j][k];
        fie[i][j + 1][k] %= MOD;
      }
    }
  }
}

int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    G[a].push_back(b);
    rG[b].push_back(a);
  }
  init();
  dfs(0, -1);
  dfs2(pa, -1);
  solve(pa, -1);
  ll ans = 0;
  for (int i = 0; i <= n; i++) {
    ans += dp[pa][i];
    ans %= MOD;
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n;
vector<int> G[201];
vector<int> rG[201];
ll fie[201][201][201];
ll dp[201][201];
ll nxt[201];
ll comb[205][205];
int pa;
int siz[201];

void solve(int v, int p) {
  int ns = 0;
  dp[v][0] = 1;
  for (int i = 0; i < G[v].size(); i++) {
    int nv = G[v][i];
    if (nv != p) {
      solve(nv, v);
      memset(nxt, 0, sizeof(nxt));
      for (int j = 0; j <= ns; j++) {
        for (int k = 0; k <= siz[nv]; k++) {
          for (int l = 0; l <= min(j, k); l++) {
            ll val = dp[v][j] * dp[nv][k] % MOD;
            val = (val * fie[j][k][l]) % MOD;
            // printf("%d %d %d %lld %lld %lld\\n",j,k,l,dp[v][j],dp[nv][k],val);
            nxt[j + k - l] += val;
            nxt[j + k - l] %= MOD;
          }
        }
      }
      ns += siz[nv];
      for (int j = 0; j <= ns; j++) {
        dp[v][j] = nxt[j];
      }
    }
  }
  for (int i = ns; i >= 0; i--) {
    dp[v][i + 1] = dp[v][i];
  }
  dp[v][0] = 0;
}

void dfs(int v, int p) {
  pa = v;
  for (int i = 0; i < rG[v].size(); i++) {
    int nv = rG[v][i];
    if (nv != p) {
      dfs(G[v][i], v);
    }
  }
}
int dfs2(int v, int p) {
  int ans = 1;
  for (int i = 0; i < G[v].size(); i++) {
    int nv = G[v][i];
    if (nv != p) {
      ans += dfs2(G[v][i], v);
    }
  }
  return (siz[v] = ans);
}

void init() {
  fie[0][0][0] = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        fie[i + 1][j][k] += fie[i][j][k];
        fie[i + 1][j][k] %= MOD;
        fie[i + 1][j + 1][k + 1] += fie[i][j][k];
        fie[i + 1][j + 1][k + 1] %= MOD;
        fie[i][j + 1][k] += fie[i][j][k];
        fie[i][j + 1][k] %= MOD;
      }
    }
  }
}

int main(void) {
  comb[0][0] = 1;
  for (int i = 1; i <= 200; i++) {
    for (int j = 1; j < i; j++) {
      comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % MOD;
    }
    comb[i][0] = 1;
    comb[i][i] = 1;
  }
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    G[a].push_back(b);
    rG[b].push_back(a);
  }
  init();
  dfs(0, -1);
  dfs2(pa, -1);
  solve(pa, -1);
  ll ans = 0;
  for (int i = 0; i <= n; i++) {
    ans += dp[pa][i];
    ans %= MOD;
  }
  printf("%lld\\n", ans);
  return 0;
}