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

#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < int(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;

int dp[25][25][26][26];

int main() {
  while (1) {
    int n, m, r;
    cin >> n >> m >> r;
    if (!n)
      break;
    deque<int> a(n), b(m);
    REP(i, n) cin >> a[i];
    REP(i, m) cin >> b[i];
    multimap<pair<int, int>, int> rules;
    int count = 31;
    REP(i, r) {
      int k;
      cin >> k;
      vector<int> x(k);
      REP(j, k) cin >> x[j];
      int y;
      cin >> y;
      if (k >= 3) {
        rules.emplace(make_pair(x[0], x[1]), count);
        ++count;
        REP(j, k - 3) {
          rules.emplace(make_pair(count - 1, x[j + 2]), count);
          ++count;
        }
        rules.emplace(make_pair(count - 1, x.back()), y);
        ++count;
      } else {
        rules.emplace(make_pair(x[0], x[1]), y);
      }
    }
    using S = vector<int>;
    S as, bs;
    vector<vector<S>> cyka(n, vector<S>(n + 1));
    REP(j, n) { cyka[j][1].push_back(a[j]); }
    FOR(l, 2, n + 1) REP(j, n) {
      FOR(k, 1, l) {
        for (const auto &sl : cyka[j][k]) {
          for (const auto &sr : cyka[(j + k) % n][l - k]) {
            auto pitr = rules.equal_range(make_pair(sl, sr));
            for (auto itr = pitr.first; itr != pitr.second; ++itr) {
              cyka[j][l].push_back(itr->second);
            }
          }
        }
      }
      sort(ALL(cyka[j][l]));
      cyka[j][l].erase(unique(ALL(cyka[j][l])), end(cyka[j][l]));
    }
    vector<vector<S>> cykb(m, vector<S>(m + 1));
    REP(j, m) { cykb[j][1].push_back(b[j]); }
    FOR(l, 2, m + 1) REP(j, m) {
      FOR(k, 1, l) {
        for (const auto &sl : cykb[j][k]) {
          for (const auto &sr : cykb[(j + k) % m][l - k]) {
            auto pitr = rules.equal_range(make_pair(sl, sr));
            for (auto itr = pitr.first; itr != pitr.second; ++itr) {
              cykb[j][l].push_back(itr->second);
            }
          }
        }
      }
      sort(ALL(cykb[j][l]));
      cykb[j][l].erase(unique(ALL(cykb[j][l])), end(cykb[j][l]));
    }
    FOR(la, 1, n + 1) FOR(lb, 1, m + 1) REP(i, n) REP(j, m) {
      vector<int> its;
      set_intersection(ALL(cyka[i][la]), ALL(cykb[j][lb]), back_inserter(its));
      if (its.empty()) {
        dp[i][j][la][lb] = 0;
      } else {
        dp[i][j][la][lb] = 1;
      }
    }
    FOR(la, 2, n + 1) FOR(lb, 2, m + 1) REP(i, n) REP(j, m) {
      FOR(ka, 1, la) FOR(kb, 1, lb) {
        if (dp[i][j][ka][kb] &&
            dp[(i + ka) % n][(j + kb) % m][la - ka][lb - kb]) {
          dp[i][j][la][lb] =
              max(dp[i][j][la][lb],
                  dp[i][j][ka][kb] +
                      dp[(i + ka) % n][(j + kb) % m][la - ka][lb - kb]);
        }
      }
    }
    int mx = 0;
    REP(i, n) REP(j, m) mx = max(mx, dp[i][j][n][m]);
    if (mx) {
      cout << mx << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < int(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;

int dp[25][25][26][26];

int main() {
  while (1) {
    int n, m, r;
    cin >> n >> m >> r;
    if (!n)
      break;
    deque<int> a(n), b(m);
    REP(i, n) cin >> a[i];
    REP(i, m) cin >> b[i];
    multimap<pair<int, int>, int> rules;
    int count = 31;
    REP(i, r) {
      int k;
      cin >> k;
      vector<int> x(k);
      REP(j, k) cin >> x[j];
      int y;
      cin >> y;
      if (k >= 3) {
        rules.emplace(make_pair(x[0], x[1]), count);
        ++count;
        REP(j, k - 3) {
          rules.emplace(make_pair(count - 1, x[j + 2]), count);
          ++count;
        }
        rules.emplace(make_pair(count - 1, x.back()), y);
        ++count;
      } else {
        rules.emplace(make_pair(x[0], x[1]), y);
      }
    }
    using S = vector<int>;
    S as, bs;
    vector<vector<S>> cyka(n, vector<S>(n + 1));
    REP(j, n) { cyka[j][1].push_back(a[j]); }
    FOR(l, 2, n + 1) REP(j, n) {
      FOR(k, 1, l) {
        for (const auto &sl : cyka[j][k]) {
          for (const auto &sr : cyka[(j + k) % n][l - k]) {
            auto pitr = rules.equal_range(make_pair(sl, sr));
            for (auto itr = pitr.first; itr != pitr.second; ++itr) {
              cyka[j][l].push_back(itr->second);
            }
          }
        }
      }
      sort(ALL(cyka[j][l]));
      cyka[j][l].erase(unique(ALL(cyka[j][l])), end(cyka[j][l]));
    }
    vector<vector<S>> cykb(n, vector<S>(n + 1));
    REP(j, m) { cykb[j][1].push_back(b[j]); }
    FOR(l, 2, m + 1) REP(j, m) {
      FOR(k, 1, l) {
        for (const auto &sl : cykb[j][k]) {
          for (const auto &sr : cykb[(j + k) % m][l - k]) {
            auto pitr = rules.equal_range(make_pair(sl, sr));
            for (auto itr = pitr.first; itr != pitr.second; ++itr) {
              cykb[j][l].push_back(itr->second);
            }
          }
        }
      }
      sort(ALL(cykb[j][l]));
      cykb[j][l].erase(unique(ALL(cykb[j][l])), end(cykb[j][l]));
    }
    FOR(la, 1, n + 1) FOR(lb, 1, m + 1) REP(i, n) REP(j, m) {
      vector<int> its;
      set_intersection(ALL(cyka[i][la]), ALL(cykb[j][lb]), back_inserter(its));
      if (its.empty()) {
        dp[i][j][la][lb] = 0;
      } else {
        dp[i][j][la][lb] = 1;
      }
    }
    FOR(la, 2, n + 1) FOR(lb, 2, m + 1) REP(i, n) REP(j, m) {
      FOR(ka, 1, la) FOR(kb, 1, lb) {
        if (dp[i][j][ka][kb] &&
            dp[(i + ka) % n][(j + kb) % m][la - ka][lb - kb]) {
          dp[i][j][la][lb] =
              max(dp[i][j][la][lb],
                  dp[i][j][ka][kb] +
                      dp[(i + ka) % n][(j + kb) % m][la - ka][lb - kb]);
        }
      }
    }
    int mx = 0;
    REP(i, n) REP(j, m) mx = max(mx, dp[i][j][n][m]);
    if (mx) {
      cout << mx << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}