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

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

const int MAXN = 55;

bool w[MAXN][MAXN], f[MAXN][MAXN][105];
int vis[MAXN];

int main() {
  int n, m, len[3];
  scanf("%d%d", &n, &m);
  memset(w, 0, sizeof(w));
  memset(f, 0, sizeof(f));
  memset(vis, 0, sizeof(vis));
  for (int i = 0; i < 3; i++)
    scanf("%d", &len[i]);
  for (int u, v; m--;) {
    scanf("%d%d", &u, &v);
    w[u][v] = true;
  }
  for (int i = 1; i <= n; i++) {
    f[i][i][0] = true;
    for (int j = 1; j <= 100; j++)
      for (int k = 1; k <= n; k++)
        for (int l = 1; l <= n; l++)
          if (w[l][k] && f[i][l][j - 1])
            f[i][k][j] = true;
  }
  fill(vis + 1, vis + n, INT_MAX);
  for (int now = 1; now <= n; now++)
    for (int i = 1; i <= n; i++)
      if (vis[i] > now) {
        bool flag;
        for (int j = 0; j < 3; j++) {
          flag = false;
          for (int k = 1; k <= n; k++)
            if (vis[k] < now && f[i][k][len[j]]) {
              flag = true;
              break;
            }
          if (!flag)
            break;
        }
        if (flag) {
          if (i == 1) {
            printf("%d\\n", now);
            return 0;
          }
          vis[i] = now;
        }
      }
  puts("IMPOSSIBLE");
  return 0;
}

Error: 0

Fixed submission:

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

typedef double DB;
typedef long long LL;

int n, m, a, b, c;
int g[55][55];
LL f[222][211], h[222][211];

map<LL, int> show;

int dfs(int d, LL sta) {
  if (sta & (1LL << (n - 1)))
    return d;
  // if (d > 1000) return -1;
  if (show[sta] == 10) {
    show[sta] = 0;
    return -1;
  }
  show[sta] += 1;
  LL sa = 0, sb = 0, sc = 0;
  for (int i = 0; i < n; ++i) {
    if (sta & (1LL << i)) {
      sa |= f[i][a];
      sb |= f[i][b];
      sc |= f[i][c];
    }
  }
  if (sa == 0 || sb == 0 || sc == 0) {
    show[sta] = 0;
    return -1;
  }

  int a1 = dfs(d + 1, sa);
  int a2 = 0, a3 = 0;
  if (a1 == -1) {
    show[sta] = 0;
    return -1;
  }

  if (a != b) {
    a2 = dfs(d + 1, sb);
    if (a2 == -1) {
      show[sta] = 0;
      return -1;
    }
  }

  if (a != c && b != c) {
    a3 = dfs(d + 1, sc);
    if (a3 == -1) {
      show[sta] = 0;
      return -1;
    }
  }
  show[sta] = 0;

  return max(a1, max(a2, a3));
}

int main() {
#ifdef lol
  freopen("hd.in", "r", stdin);
  freopen("hd.out", "w", stdout);
#endif

  scanf("%d %d %d %d %d", &n, &m, &a, &b, &c);
  memset(f, 0, sizeof f);
  for (int i = 0; i < n; ++i)
    f[i][0] = 1LL << i, h[i][0] = 1LL << i;
  for (int i = 1; i <= m; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    --u, --v;
    g[u][v] = 1;
    f[u][1] |= 1LL << v;
    h[v][1] |= 1LL << u;
  }
  for (int k = 2; k <= 100; ++k)
    for (int j = 1; j < k; ++j)
      for (int t = 0; t < n; ++t)
        for (int p = 0; p < n; ++p)
          if (f[t][j] & h[p][k - j])
            f[t][k] |= 1LL << p, h[p][k] |= 1LL << t;
  for (int i = 0; i < n; ++i) {
    for (int k = 0; k <= 100; ++k) {
      for (int j = 0; j < n; ++j)
        if (f[i][k] & (1 << j)) {
          assert((h[j][k] & (1 << i)) != 0);
        }
    }
  }
  int ans = dfs(0, 1LL);

  if (ans == -1) {
    puts("IMPOSSIBLE");
  } else {
    printf("%d\\n", ans);
  }

  return 0;
}