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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &o) {
  out << "(" << o.first << "," << o.second << ")";
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> V) {
  for (int i = 0; i < V.size(); i++) {
    out << V[i];
    if (i != V.size() - 1)
      out << " ";
  }
  return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> Mat) {
  for (int i = 0; i < Mat.size(); i++) {
    if (i != 0)
      out << endl;
    out << Mat[i];
  }
  return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const map<S, T> mp) {
  out << "{ ";
  for (auto it = mp.begin(); it != mp.end(); it++) {
    out << it->first << ":" << it->second;
    if (mp.size() - 1 != distance(mp.begin(), it))
      out << ", ";
  }
  out << " }";
  return out;
}

/*
 <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2011>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

int n;
ll solve() {
  ll res = -1;
  vector<vector<int>> schedule(31);
  for (int i = 0; i < n; i++) {
    int f;
    cin >> f;
    for (int j = 0; j < f; j++) {
      int d;
      cin >> d;
      schedule[d].push_back(i);
    }
  }

  vector<ll> maps(n);
  for (int i = 0; i < n; i++) {
    maps[i] = (1LL << i);
  }
  for (int i = 1; i <= 30; i++) {
    int sz = (int)schedule[i].size();
    ll S = 0;
    for (int j = 0; j < sz; j++) {
      S |= (maps[schedule[i][j]]);
    }
    for (int j = 0; j < sz; j++) {
      maps[schedule[i][j]] |= S;
      if (maps[schedule[i][j]] == (1LL << n) - 1) {
        res = i;
        break;
      }
    }
    if (res != -1)
      break;
  }
  return res;
}
int main(void) {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  while (cin >> n, n) {
    cout << solve() << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &o) {
  out << "(" << o.first << "," << o.second << ")";
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> V) {
  for (int i = 0; i < V.size(); i++) {
    out << V[i];
    if (i != V.size() - 1)
      out << " ";
  }
  return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> Mat) {
  for (int i = 0; i < Mat.size(); i++) {
    if (i != 0)
      out << endl;
    out << Mat[i];
  }
  return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const map<S, T> mp) {
  out << "{ ";
  for (auto it = mp.begin(); it != mp.end(); it++) {
    out << it->first << ":" << it->second;
    if (mp.size() - 1 != distance(mp.begin(), it))
      out << ", ";
  }
  out << " }";
  return out;
}

/*
 <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2011>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

int n;
ll solve() {
  ll res = -1;
  UnionFind UF(n);
  vector<vector<int>> schedule(31);
  for (int i = 0; i < n; i++) {
    int f;
    cin >> f;
    for (int j = 0; j < f; j++) {
      int d;
      cin >> d;
      schedule[d].push_back(i);
    }
  }

  for (int i = 1; i <= 30; i++) {
    int sz = (int)schedule[i].size();
    for (int j = 0; j < sz; j++) {
      for (int k = j + 1; j < sz; j++) {
        UF.unionSet(schedule[i][j], schedule[i][k]);
      }
    }
    if (UF.size(0) == n) {
      res = i;
      break;
    }
  }
  return res;
}
int main(void) {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  while (cin >> n, n) {
    cout << solve() << endl;
  }
  return 0;
}