Problem p01136 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>

using namespace std;

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

const int INFTY = 1 << 29;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

int main() {
  for (int n; cin >> n, n;) {
    vvi ds(n);
    rep(i, n) {
      int f;
      cin >> f;
      ds[i].resize(f);
      rep(j, f) {
        cin >> ds[i][j];
        ds[i][j]--;
      }
    }

    vector<ll> meet(30);
    rep(i, n) rep(j, ds[i].size()) meet[ds[i][j]] |= 1ll << i;

    vector<ll> canhave(n);
    rep(i, n) canhave[i] |= 1ll << i;

    bool ok = false;
    for (int d = 0;; d++) {
      ll maps = 0;
      rep(i, n) if (meet[d] >> i & 1) maps |= canhave[i];
      rep(i, n) {
        if (meet[d] >> i & 1)
          canhave[i] |= maps;
        if (canhave[i] == (1ll << n) - 1) {
          cout << d + 1 << endl;
          ok = true;
          goto end;
        }
      }
    }
  end:
    if (!ok)
      cout << -1 << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>

using namespace std;

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

const int INFTY = 1 << 29;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

int main() {
  for (int n; cin >> n, n;) {
    vvi ds(n);
    rep(i, n) {
      int f;
      cin >> f;
      ds[i].resize(f);
      rep(j, f) {
        cin >> ds[i][j];
        ds[i][j]--;
      }
    }

    vector<ll> meet(30);
    rep(i, n) rep(j, ds[i].size()) meet[ds[i][j]] |= 1ll << i;

    vector<ll> canhave(n);
    rep(i, n) canhave[i] |= 1ll << i;

    bool ok = false;
    rep(d, 30) {
      ll maps = 0;
      rep(i, n) if (meet[d] >> i & 1) maps |= canhave[i];
      rep(i, n) {
        if (meet[d] >> i & 1)
          canhave[i] |= maps;
        if (canhave[i] == (1ll << n) - 1) {
          cout << d + 1 << endl;
          ok = true;
          goto end;
        }
      }
    }
  end:
    if (!ok)
      cout << -1 << endl;
  }

  return 0;
}