Problem p01136 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define sz size()
#define pb push_back
#define mp make_pair
#define ALL(X) (X).begin(), (X).end()

using namespace std;

const int INF = 1000000000;
const double eps = 1e-8;
/*
int root(vector<int> & ufa,int i)
{
        if(i != ufa[i])
                ufa[i] = root(ufa,ufa[i]);
        return ufa[i];
}

void join(vector<int> & ufa,int i,int j)
{
        int ri = root(ufa,i);
        int rj = root(ufa,j);
        ufa[ri] = rj;
}
*/
struct edge {
  int s;
  int t;
};

int main(void) {
  while (1) {
    int n;
    cin >> n;
    if (!n)
      break;
    vector<vector<int>> ary(n);
    vector<int> l(n);
    int gsize = 30 + n;
    vector<vector<int>> dist(gsize, vector<int>(gsize, INF));
    REP(i, n) {
      cin >> l[i];
      ary[i] = vector<int>(l[i]);
      REP(j, l[i]) cin >> ary[i][j];
      sort(ALL(ary[i]));
      if (l[i])
        dist[i][n + ary[i][0] - 1] = 1;
      FOR(j, 1, l[i]) { dist[n + ary[i][j - 1] - 1][n + ary[i][j] - 1] = 1; }
    }
    REP(i, gsize)
    REP(j, gsize) REP(k, gsize) dist[j][k] =
        min(dist[j][k], dist[j][i] + dist[i][k]);
    int days = -1;
    REP(i, 30) {
      bool flg = true;
      REP(j, n) {
        if (dist[j][n + i] >= INF) {
          flg = false;
          break;
        }
      }
      if (flg) {
        days = i + 1;
        break;
      }
    }
    cout << days << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define sz size()
#define pb push_back
#define mp make_pair
#define ALL(X) (X).begin(), (X).end()

using namespace std;

const int INF = 1000000000;
const double eps = 1e-8;
/*
int root(vector<int> & ufa,int i)
{
        if(i != ufa[i])
                ufa[i] = root(ufa,ufa[i]);
        return ufa[i];
}

void join(vector<int> & ufa,int i,int j)
{
        int ri = root(ufa,i);
        int rj = root(ufa,j);
        ufa[ri] = rj;
}
*/
struct edge {
  int s;
  int t;
};

int main(void) {
  while (1) {
    int n;
    cin >> n;
    if (!n)
      break;
    vector<vector<int>> ary(n);
    vector<int> l(n);
    int gsize = 30 + n;
    vector<vector<int>> dist(gsize, vector<int>(gsize, INF));
    REP(i, n) {
      cin >> l[i];
      ary[i] = vector<int>(l[i]);
      REP(j, l[i]) cin >> ary[i][j];
      sort(ALL(ary[i]));
      dist[i][n + ary[i][0] - 1] = 1;
      FOR(j, 1, l[i]) { dist[n + ary[i][j - 1] - 1][n + ary[i][j] - 1] = 1; }
    }
    REP(i, gsize)
    REP(j, gsize) REP(k, gsize) dist[j][k] =
        min(dist[j][k], dist[j][i] + dist[i][k]);
    int days = -1;
    REP(i, 30) {
      bool flg = true;
      REP(j, n) {
        if (dist[j][n + i] >= INF) {
          flg = false;
          break;
        }
      }
      if (flg) {
        days = i + 1;
        break;
      }
    }
    cout << days << endl;
  }
  return 0;
}