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

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

#define iota(i, n, b, s)                                                       \\
  for (int i = int(b); i != int((b) + (s) * (n)); i += (s))
#define range(i, n, m)                                                         \\
  iota(i, (((n) > (m)) ? ((n) - (m)) : ((m) - (n))), (n), ((n) > (m) ? -1 : 1))
#define rep(i, n) iota(i, (n), 0, 1)
#define loop for (;;)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a, b) (make_pair(a, b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr << ">>> " << #var << " = " << var << endl;

typedef long long Integer;
typedef double Real;
typedef vector<int> vi;
typedef vector<string> vs;
typedef map<string, int> Dictionary;
const Real PI = acos(-1);

template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template <class T> ostream &operator<<(ostream &os, vector<T> v) {
  if (v.size() == 0) {
    os << "(empty)";
    return os;
  }
  os << v[0];
  for (int i = 1, len = v.size(); i < len; ++i)
    os << ' ' << v[i];
  return os;
}

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int main() {

  for (int n; cin >> n, n;) {

    vector<int> schedule[32];

    rep(i, n) {
      int m;
      cin >> m;
      rep(j, m) {
        int x;
        cin >> x;
        schedule[x].push_back(i);
      }
    }

    // b[i][j] = i can have that of j
    bool b[n][n];
    rep(i, n) {
      rep(j, n) { b[i][j] = i == j; }
    }

    rep(d, 31) {
      int cx = 0;
      rep(j, n) {
        bool bl = false;
        for (int i : schedule[d]) {
          bl |= b[i][j];
        }
        for (int i : schedule[d]) {
          b[i][j] = bl;
          // cerr << i << " can have that of " << j << " on " << d << endl;
        }
        if (bl)
          ++cx;
      }
      if (cx == n) {
        cout << d << endl;
        goto more;
      }
    }
    cout << (-1) << endl;

  more:;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

#define iota(i, n, b, s)                                                       \\
  for (int i = int(b); i != int((b) + (s) * (n)); i += (s))
#define range(i, n, m)                                                         \\
  iota(i, (((n) > (m)) ? ((n) - (m)) : ((m) - (n))), (n), ((n) > (m) ? -1 : 1))
#define rep(i, n) iota(i, (n), 0, 1)
#define loop for (;;)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a, b) (make_pair(a, b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr << ">>> " << #var << " = " << var << endl;

typedef long long Integer;
typedef double Real;
typedef vector<int> vi;
typedef vector<string> vs;
typedef map<string, int> Dictionary;
const Real PI = acos(-1);

template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template <class T> ostream &operator<<(ostream &os, vector<T> v) {
  if (v.size() == 0) {
    os << "(empty)";
    return os;
  }
  os << v[0];
  for (int i = 1, len = v.size(); i < len; ++i)
    os << ' ' << v[i];
  return os;
}

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int main() {

  for (int n; cin >> n, n;) {

    vector<int> schedule[32];

    rep(i, n) {
      int m;
      cin >> m;
      rep(j, m) {
        int d;
        cin >> d;
        schedule[d].push_back(i);
      }
    }

    int d[n][n]; // distance
    rep(i, n) rep(j, n) d[i][j] = i == j ? 0 : INF;

    int when[n][n];
    rep(i, n) rep(j, n) when[i][j] = -1;

    rep(_, 32) {
      int m = schedule[_].size();
      rep(i, m) {
        range(j, i + 1, m) {
          int u = schedule[_][i];
          int v = schedule[_][j];
          d[u][v] = d[v][u] = 1;
          when[u][v] = when[v][u] = when[u][v] == -1 ? _ : min(when[u][v], _);
        }
      }
    }

    rep(k, n) rep(i, n) rep(j, n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

    // rep (i, n) rep (j, n) cerr << d[i][j] << " \\n"[j == n-1];

    {
      int u;
      int v;
      int md = -1;
      rep(i, n) {
        range(j, i + 1, n) {
          if (md < d[i][j]) {
            md = d[i][j];
            u = i;
            v = j;
          }
        }
      }

      // trace(cons( cons(u, v) , md));

      int ans = -1;

      // trace
      for (; u != v;) {
        rep(w, n) {
          if (u == w)
            continue;
          if (when[u][w] < 0)
            continue;
          if (d[u][w] != 1)
            continue;
          if (d[u][v] == d[u][w] + d[w][v]) {
            // trace(cons(cons(u, w), when[u][w]));
            ans = max(ans, when[u][w]);
            u = w;
            break;
          }
        }
      }

      cout << ans << endl;
    }
  }

  return 0;
}