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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
int main() {
  while (1) {
    int n[2], r;
    int a[25][2];
    vector<int> ch[65];
    for (int i = 0; i < 65; i++)
      ch[i].clear();
    cin >> n[0] >> n[1] >> r;
    if (n[0] == 0)
      return 0;
    rep(q, 2) for (int i = 0; i < n[q]; i++) cin >> a[i][q];
    for (int i = 0; i < r; i++) {
      int k;
      cin >> k;
      for (int j = 0; j < k; j++) {
        int x;
        cin >> x;
        ch[i].pb(x);
      }
      int y;
      cin >> y;
      ch[i].pb(y);
    }
    bool dp[30][30][35][2] = {};
    bool ok[30][30][30][30] = {};
    for (int q = 0; q < 2; q++) {
      for (int i = 0; i < n[q]; i++)
        dp[i][i][a[i][q]][q] = 1;
      while (1) {
        bool upd = 0;
        for (int i = 0; i < r; i++) {
          for (int j = 1; j <= n[q]; j++) {
            bool ok[11][25] = {};
            ok[0][j - 1] = 1;
            for (int k = 0; k < ch[i].size() - 1; k++) {
              for (int s = 0; s < n[q]; s++) {
                if (!ok[k][s])
                  continue;
                for (int t = s + 1; t <= s + n[q]; t++) {
                  if ((t - j) % n[q] == 0 && k)
                    break;
                  if (dp[(s + 1) % n[q]][t % n[q]][ch[i][k]][q] &&
                      !ok[k + 1][t % n[q]]) {
                    ok[k + 1][t % n[q]] = 1;
                  }
                }
              }
            }
            for (int k = 0; k < n[q]; k++) {
              if (ok[ch[i].size() - 1][k] &&
                  !dp[j % n[q]][k][ch[i][ch[i].size() - 1]][q]) {
                dp[j % n[q]][k][ch[i][ch[i].size() - 1]][q] = 1;
                upd = 1;
              }
            }
          }
        }
        if (!upd)
          break;
      }
    }
    for (int i = 0; i < n[0]; i++)
      for (int j = 0; j < n[0]; j++)
        for (int k = 0; k < n[1]; k++)
          for (int l = 0; l < n[1]; l++)
            for (int x = 1; x <= 30; x++) {
              if (dp[i][j][x][0] && dp[k][l][x][1])
                ok[i][j][k][l] = 1;
            }
    int L[30][30][30][30];
    rep(a, 30) rep(b, 30) rep(c, 30) rep(d, 30) L[a][b][c][d] = INF;
    // syokika
    for (int x = 1; x <= 30; x++) {
      for (int ii = 0; ii < n[0]; ii++) {
        for (int i = 0; i < n[1]; i++) {
          bool G = 0;
          for (int a = ii; (a != ii || !G); a = (a + 1) % n[0]) {
            G = 1;
            if (!dp[ii][a][x][0])
              continue;
            bool F = 0;
            for (int b = i; (b != i || !F); b = (b + 1) % n[1]) {
              F = 1;
              if (!dp[i][b][x][1])
                continue;
              L[ii][a][i][b] = 1;
            }
          }
        }
      }
    } // if(n[0]==16)return 0;
    // if(n[0]==16) cout << L[14][4][5][5] << endl;
    int ans = -1;
    for (int ii = 0; ii < n[0]; ii++) {
      for (int i = 0; i < n[1]; i++) {
        // if(ii==14&&i==5) cout << L[14][4][5][5] << endl;
        bool G = 0;
        for (int j = ii; (j != ii || !G);
             j = (j + 1) % n[0]) { // cout << "K" << endl;
          G = 1;
          bool F = 0;
          for (int k = i; (k != i || !F); k = (k + 1) % n[1]) {
            F = 1;
            if (L[ii][j][i][k] > 1000)
              continue;
            // if(ii==14&&i==5&&j==4&&k==5) cout << "j" << endl;
            {
              for (int J = (j + 1) % n[0]; (J != ii); J = (J + 1) % n[0]) {
                for (int K = (k + 1) % n[1]; (K != i); K = (K + 1) % n[1]) {
                  if (ok[(j + 1) % n[0]][J][(k + 1) % n[1]][K]) {
                    if (L[ii][J][i][K] == INF)
                      L[ii][J][i][K] = -1;
                    L[ii][J][i][K] = max(L[ii][J][i][K], L[ii][j][i][k] + 1);
                    // if(ii==14&&J==5&&i==5&&K==10)cout<<L[ii][J][i][K]<<endl;
                  }
                }
              }
            }
          }
        }
        if (L[ii][(ii + n[0] - 1) % n[0]][i][(i + n[1] - 1) % n[1]] <= 1000)
          ans =
              max(ans, L[ii][(ii + n[0] - 1) % n[0]][i][(i + n[1] - 1) % n[1]]);
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
int main() {
  while (1) {
    int n[2], r;
    int a[25][2];
    vector<int> ch[65];
    for (int i = 0; i < 65; i++)
      ch[i].clear();
    cin >> n[0] >> n[1] >> r;
    if (n[0] == 0)
      return 0;
    rep(q, 2) for (int i = 0; i < n[q]; i++) cin >> a[i][q];
    for (int i = 0; i < r; i++) {
      int k;
      cin >> k;
      for (int j = 0; j < k; j++) {
        int x;
        cin >> x;
        ch[i].pb(x);
      }
      int y;
      cin >> y;
      ch[i].pb(y);
    }
    bool dp[30][30][35][2] = {};
    for (int q = 0; q < 2; q++) {
      for (int i = 0; i < n[q]; i++)
        dp[i][i][a[i][q]][q] = 1;
      while (1) {
        bool upd = 0;
        for (int i = 0; i < r; i++) {
          for (int j = 1; j <= n[q]; j++) {
            bool ok[11][25] = {};
            ok[0][j - 1] = 1;
            for (int k = 0; k < ch[i].size() - 1; k++) {
              for (int s = 0; s < n[q]; s++) {
                if (!ok[k][s])
                  continue;
                for (int t = s + 1; t <= s + n[q]; t++) {
                  if ((t - j) % n[q] == 0 && k)
                    break;
                  if (dp[(s + 1) % n[q]][t % n[q]][ch[i][k]][q] &&
                      !ok[k + 1][t % n[q]]) {
                    ok[k + 1][t % n[q]] = 1;
                  }
                }
              }
            }
            for (int k = 0; k < n[q]; k++) {
              if (ok[ch[i].size() - 1][k] &&
                  !dp[j % n[q]][k][ch[i][ch[i].size() - 1]][q]) {
                dp[j % n[q]][k][ch[i][ch[i].size() - 1]][q] = 1;
                upd = 1;
              }
            }
          }
        }
        if (!upd)
          break;
      }
    }
    /*		if(n[0]==16){
                        cout <<
       dp[5][5][3][0]<<dp[6][6][2][0]<<dp[7][7][1][0]<<dp[8][8][3][0]<<dp[9][11][2][0]<<
       dp[12][12][1][0]<<dp[13][13][3][0]<<dp[14][4][2][0]<<dp[6][10][3][1] <<
       dp[11][11][2][1]<<dp[12][12][1][1]<<dp[13][13][3][1]<<dp[0][0][2][1]<<dp[1][1][1][1]<<dp[2][4][3][1]<<dp[5][5][2][1]<<endl;
                    }*/
    // cout << dp[2][0][3][1] << "f " << endl;
    int L[30][30][30][30];
    rep(a, 30) rep(b, 30) rep(c, 30) rep(d, 30) L[a][b][c][d] = INF;
    // syokika
    // if(n[0]==16)return 0;
    for (int x = 1; x <= 30; x++) {
      for (int ii = 0; ii < n[0]; ii++) {
        for (int i = 0; i < n[1]; i++) {
          bool G = 0;
          for (int a = ii; (a != ii || !G); a = (a + 1) % n[0]) {
            G = 1;
            if (!dp[ii][a][x][0])
              continue;
            bool F = 0;
            for (int b = i; (b != i || !F); b = (b + 1) % n[1]) {
              F = 1;
              if (!dp[i][b][x][1])
                continue;
              L[ii][a][i][b] = 1;
            }
          }
        }
      }
    } // if(n[0]==16)return 0;
    // if(n[0]==16) cout << L[14][4][5][5] << endl;
    int ans = -1;
    for (int ii = 0; ii < n[0]; ii++) {
      for (int i = 0; i < n[1]; i++) {
        // if(ii==14&&i==5) cout << L[14][4][5][5] << endl;
        bool G = 0;
        for (int j = ii; (j != ii || !G);
             j = (j + 1) % n[0]) { // cout << "K" << endl;
          G = 1;
          bool F = 0;
          for (int k = i; (k != i || !F); k = (k + 1) % n[1]) {
            F = 1;
            if (L[ii][j][i][k] > 1000)
              continue;
            // if(ii==14&&i==5&&j==4&&k==5) cout << "j" << endl;
            for (int x = 1; x <= 30; x++) {
              for (int J = (j + 1) % n[0]; (J != ii); J = (J + 1) % n[0]) {
                for (int K = (k + 1) % n[1]; (K != i); K = (K + 1) % n[1]) {
                  if (dp[(j + 1) % n[0]][J][x][0] &&
                      dp[(k + 1) % n[1]][K][x][1]) {
                    if (L[ii][J][i][K] == INF)
                      L[ii][J][i][K] = -1;
                    L[ii][J][i][K] = max(L[ii][J][i][K], L[ii][j][i][k] + 1);
                    // if(ii==14&&J==5&&i==5&&K==10)cout<<L[ii][J][i][K]<<endl;
                  }
                }
              }
            }
          }
        }
        if (L[ii][(ii + n[0] - 1) % n[0]][i][(i + n[1] - 1) % n[1]] <= 1000)
          ans =
              max(ans, L[ii][(ii + n[0] - 1) % n[0]][i][(i + n[1] - 1) % n[1]]);
      }
    }
    cout << ans << endl;
  }
}