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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef ll li;
typedef pair<int, int> PI;
#define EPS (1e-10L)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define F first
#define S second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define FOR(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
void pkuassert(bool t) { t = 1 / t; };
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};

bool hima[50][40];
int co[50][50];
int n;

void solve() {
  memset(hima, 0, sizeof(hima));
  memset(co, -1, sizeof(co));

  rep(i, n) {
    int fi;
    cin >> fi;
    rep(j, fi) {
      int t;
      cin >> t;
      hima[i][t] = 1;
    }
  }
  rep(i, n) rep(j, n) co[i][j] = 100;
  rep(i, n) co[i][i] = 0;

  rep(i, n) {
    rep(j, 33) {
      rep(k, n) rep(l, n) if (hima[k][j] && hima[l][j] && co[i][k] < j)
          co[i][l] = min(co[i][l], j);
    }
  }

  int ans = 100;
  rep(i, n) {
    int ma = 0;
    bool ok = true;
    rep(j, n) {
      if (co[j][i] == 100) {
        ok = false;
        break;
      }
      ma = max(co[j][i], ma);
    }
    if (ok)
      ans = min(ma, ans);
  }

  if (ans != 100)
    cout << ans << endl;
  else
    cout << -1 << endl;
}

int main(int argc, char *argv[]) {
  while (cin >> n && n)
    solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef ll li;
typedef pair<int, int> PI;
#define EPS (1e-10L)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define F first
#define S second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define FOR(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
void pkuassert(bool t) { t = 1 / t; };
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};

bool hima[50][40];
int n;

void solve() {
  memset(hima, 0, sizeof(hima));

  rep(i, n) {
    int fi;
    cin >> fi;
    rep(j, fi) {
      int t;
      cin >> t;
      hima[i][t] = 1;
    }
  }

  set<int> co[50][50];

  priority_queue<pair<int, PI>> q;
  rep(i, n) q.push(mp(0, mp(i, i)));
  while (!q.empty()) {
    int cc = -q.top().F;
    int st = q.top().S.F;
    int cv = q.top().S.S;
    q.pop();
    if (co[st][cv].count(cc))
      continue;
    // cout << cc << ' ' << st << ' ' << cv << endl;
    co[st][cv].insert(cc);
    if (cc < 33)
      q.push(mp(-cc - 1, mp(st, cv)));
    for (int ne = cc; ne < 31; ++ne)
      if (hima[cv][ne]) {
        rep(i, n) if (hima[i][ne] && !co[st][i].count(ne))
            q.push(mp(-ne, mp(st, i)));
        break;
      }
  }

  int ans = 100;
  rep(i, n) {
    int ma = 0;
    bool ok = true;
    rep(j, n) {
      if (co[j][i].empty()) {
        ok = false;
        break;
      }
      ma = max(*co[j][i].begin(), ma);
    }
    if (ok)
      ans = min(ma, ans);
  }

  if (ans != 100)
    cout << ans << endl;
  else
    cout << -1 << endl;
}

int main(int argc, char *argv[]) {
  while (cin >> n && n)
    solve();
  return 0;
}