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

#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;
bool ok[50][30];
int bfs[50];
int v[50];
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < a; i++)
      for (int j = 0; j < 30; j++)
        ok[i][j] = false;
    int st = 0;
    for (int i = 0; i < a; i++) {
      int b;
      scanf("%d", &b);
      int val = 999999;
      for (int j = 0; j < b; j++) {
        int c;
        scanf("%d", &c);
        val = min(val, c - 1);
        ok[i][c - 1] = true;
      }
      st = max(st, val);
    }
    int ret = 999999;
    for (int i = 0; i < a; i++) {
      for (int j = st; j < 30; j++) {
        for (int k = 0; k < a; k++) {
          v[k] = 0;
          bfs[k] = -1;
        }
        priority_queue<pair<int, int>> Q;
        Q.push(make_pair(j, i));
        bfs[i] = j;
        while (Q.size()) {
          int day = Q.top().first;
          int at = Q.top().second;
          //	printf("%d %d\\n",day,at);
          Q.pop();
          if (v[at])
            continue;
          v[at] = 1;
          for (int k = 0; k < a; k++) {
            if (v[k])
              continue;
            for (int l = day; l >= 0; l--) {
              if (ok[at][l] && ok[k][l] && bfs[k] < l) {
                bfs[k] = l;
                Q.push(make_pair(l, k));
                break;
              }
            }
          }
        }
        bool dame = false;
        for (int k = 0; k < a; k++)
          if (!v[k])
            dame = true;
        if (!dame) {
          ret = min(ret, j + 1);
          //	printf("%d %d\\n",i,j);
          break;
        }
      }
    }
    if (ret == 999999)
      printf("-1\\n");
    else
      printf("%d\\n", ret);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;
bool ok[50][30];
int bfs[50];
int v[50];
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < a; i++)
      for (int j = 0; j < 30; j++)
        ok[i][j] = false;
    for (int i = 0; i < a; i++) {
      int b;
      scanf("%d", &b);
      for (int j = 0; j < b; j++) {
        int c;
        scanf("%d", &c);
        ok[i][c - 1] = true;
      }
    }
    int ret = 999999;
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < 30; j++) {
        for (int k = 0; k < a; k++) {
          v[k] = 0;
          bfs[k] = -1;
        }
        priority_queue<pair<int, int>> Q;
        Q.push(make_pair(j, i));
        bfs[i] = j;
        while (Q.size()) {
          int day = Q.top().first;
          int at = Q.top().second;
          //	printf("%d %d\\n",day,at);
          Q.pop();
          if (v[at])
            continue;
          v[at] = 1;
          for (int k = 0; k < a; k++) {
            if (v[k])
              continue;
            for (int l = day; l >= 0; l--) {
              if (ok[at][l] && ok[k][l] && bfs[k] < l) {
                bfs[k] = l;
                Q.push(make_pair(l, k));
                break;
              }
            }
          }
        }
        bool dame = false;
        for (int k = 0; k < a; k++)
          if (!v[k])
            dame = true;
        if (!dame) {
          ret = min(ret, j + 1);
          //	printf("%d %d\\n",i,j);
          break;
        }
      }
    }
    if (ret == 999999)
      printf("-1\\n");
    else
      printf("%d\\n", ret);
  }
}