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

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

#define MAX_N 1000

//// UnionFind
struct UnionFind {
  vector<int> v;
  UnionFind(int n) : v(n, -1) {}

  int find(int x) { return v[x] < 0 ? x : v[x] = find(v[x]); }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (-v[x] < -v[y])
      swap(x, y);
    v[x] += v[y];
    v[y] = x;
    return true;
  }
  bool root(int x) { return v[x] < 0; }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -v[find(x)]; }
};
int n;
vector<int> G[33];
int res[55];

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    rep(i, 33) G[i].clear();
    rep(i, n) {
      int f;
      cin >> f;
      rep(j, f) {
        int d;
        cin >> d;
        d--;
        G[d].pb(i);
      }
    }
    rep(i, n) res[i] = INF;
    rep(i, n) rep(d, 30) {
      UnionFind uf(n);
      for (int dd = d; dd >= 0; dd--) {
        rep(j, G[dd].size()) rep(k, j) {
          if (uf.same(i, G[dd][j]) || uf.same(i, G[dd][k]))
            uf.unite(G[dd][j], G[dd][k]);
        }
      }
      if (uf.size(i) == n) {
        res[i] = d;
        // dbg(d);
        break;
      }
    }
    int ans = *min_element(res, res + n);
    if (ans == INF)
      cout << "-1" << endl;
    else
      cout << ans + 1 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

#define MAX_N 1000

//// UnionFind
struct UnionFind {
  vector<int> v;
  UnionFind(int n) : v(n, -1) {}

  int find(int x) { return v[x] < 0 ? x : v[x] = find(v[x]); }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (-v[x] < -v[y])
      swap(x, y);
    v[x] += v[y];
    v[y] = x;
    return true;
  }
  bool root(int x) { return v[x] < 0; }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -v[find(x)]; }
};
int n;
vector<int> G[33];

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    rep(i, n) G[i].clear();
    rep(i, n) {
      int f;
      cin >> f;
      rep(j, f) {
        int d;
        cin >> d;
        d--;
        G[d].pb(i);
      }
    }
    UnionFind uf(n);
    int res = 0;
    rep(i, 30) {
      rep(j, G[i].size()) rep(k, j) { uf.unite(G[i][j], G[i][k]); }
      if (uf.size(0) == n) {
        res = i + 1;
        break;
      }
    }
    cout << res << endl;
  }
  return 0;
}