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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#define MAX_D 70
#define MAX_N 60
#define INF 1 << 30

int X[MAX_D][MAX_D];
int DP[MAX_D][MAX_D];
int Y[MAX_D][MAX_D];
int n, p, a;

int main() {
  while (true) {
    for (int i = 0; i < MAX_D; i++) {
      for (int j = 0; j < MAX_D; j++) {
        X[i][j] = 0;
        Y[i][j] = INF;
      }
    }
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> p;
      for (int j = 0; j < p; j++) {
        cin >> a;
        X[a][i] = 1;
      }
    }
    int res = INF;
    int res1 = 0;
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < MAX_D; k++) {
        for (int l = 0; l < MAX_D; l++) {
          DP[k][l] = 0;
        }
      }
      DP[0][j] = 1;

      for (int k = 1; k < MAX_D; k++) {
        for (int l = 0; l < n; l++) {
          if (DP[k - 1][l] == 1) {
            DP[k][l] = 1;
          }
        }
        vector<int> V;
        int ok = 0;
        for (int l = 0; l < n; l++) {
          if (X[k][l] == 1) {
            if (DP[k - 1][l] == 1) {
              ok = 1;
            }
            V.push_back(l);
          }
        }
        if (ok == 1) {
          for (int l = 0; l < V.size(); l++) {
            DP[k][V[l]] = 1;
          }
        }
      }
      for (int k = 0; k < n; k++) {
        for (int l = 0; l < MAX_N; l++) {
          if (DP[l][k] == 1) {
            Y[j][k] = min(Y[j][k], l);
          }
        }
      }
    }
    for (int i = 0; i < n; i++) {
      int res2 = 0;
      for (int j = 0; j < n; j++) {
        res2 = max(res2, Y[j][i]);
      }
      res = min(res, res2);
    }
    if (res >= INF) {
      cout << "-1" << endl;
    } else {
      cout << res << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

#define MAX_D 70
#define MAX_N 60
#define INF 1 << 30

int X[MAX_D][MAX_D];
int DP[MAX_D][MAX_D];
int Y[MAX_D][MAX_D];
int n, p, a;

int main() {
  while (true) {
    for (int i = 0; i < MAX_D; i++) {
      for (int j = 0; j < MAX_D; j++) {
        X[i][j] = 0;
        Y[i][j] = INF;
      }
    }
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> p;
      for (int j = 0; j < p; j++) {
        cin >> a;
        X[i][a] = 1;
      }
    }
    int res = INF;
    int res1 = 0;
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < MAX_D; k++) {
        for (int l = 0; l < MAX_D; l++) {
          DP[k][l] = 0;
        }
      }
      DP[0][j] = 1;

      for (int k = 1; k < MAX_D; k++) {
        for (int l = 0; l < n; l++) {
          DP[k][l] = DP[k - 1][l];
        }
        for (int h = 0; h < n; h++) {
          for (int l = 0; l < n; l++) {
            if (DP[k][l] == 1) {
              for (int m = 0; m < n; m++) {
                if (X[m][k] == 1 && X[l][k] == 1) {
                  DP[k][m] = 1;
                }
              }
            }
          }
        }
      }
      for (int k = 0; k < n; k++) {
        for (int l = 0; l < MAX_N; l++) {
          if (DP[l][k] == 1) {
            Y[j][k] = min(Y[j][k], l);
          }
        }
      }
    }
    for (int i = 0; i < n; i++) {
      int res2 = 0;
      for (int j = 0; j < n; j++) {
        res2 = max(res2, Y[i][j]);
      }
      res = min(res, res2);
    }
    if (res >= INF) {
      cout << "-1" << endl;
    } else {
      cout << res << endl;
    }
  }
  return 0;
}