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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(t, i, c) for (t::iretator i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);

typedef vector<ll> vl;

int main() {
  ll n;

  while (cin >> n, n) {
    vector<vl> table(n, vl(31));
    vvi date(n, vi(31));

    int f, d;
    REP(i, n) {
      cin >> f;
      REP(j, f) {
        cin >> d;
        date[i][d] = 1;
      }
    }

    REP(i, n) { table[i][0] = ((ll)1 << i); }

    const ll comp = ((ll)1 << n) - 1;
    int ans = -1;

    FOR(d, 1, 30) {
      ll now = 0;
      REP(m, n) {
        if (date[m][d]) {
          now |= table[m][d - 1];
        }
      }

      if (now == comp) {
        ans = d;
        break;
      }

      REP(m, n) {
        if (date[m][d]) {
          table[m][d] = now;
        } else {
          table[m][d] = table[m][d - 1];
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(t, i, c) for (t::iretator i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, n - 1)
const double EPS = 1e-10;
const double PI = acos(-1.0);

int main() {
  int n;
  while (cin >> n, n) {
    vvi date(n, vi(30 + 1));
    vector<vector<set<int>>> dp(n, vector<set<int>>(30 + 1));

    int d, t;
    REP(i, n) {
      cin >> d;
      REP(j, d) {
        cin >> t;
        date[i][t] = 1;
      }
    }

    FOR(d, 1, 30) {
      vi mem;
      REP(m, n) {
        if (date[m][d]) {
          mem.push_back(m);
        }
      }
      REP(m, n) {
        dp[m][d] = dp[m][d - 1];
        REP(j, mem.size()) { dp[m][d].insert(mem[j]); }
        if (dp[m][d].size() == n) {
          cout << d << endl;
          goto next;
        }
      }
    }

    cout << "-1" << endl;
  next:;
  }

  return 0;
}