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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y))
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int const MOD = 1000000007;
ll mod_pow(ll x, ll n) {
  return (!n) ? 1 : (mod_pow((x * x) % MOD, n / 2) * ((n & 1) ? x : 1)) % MOD;
}
int madd(int a, int b) { return (a + b) % MOD; }
int msub(int a, int b) { return (a - b + MOD) % MOD; }
int mmul(int a, int b) { return (a * b) % MOD; }
int minv(int a) { return mod_pow(a, MOD - 2); }
int mdiv(int a, int b) { return mmul(a, minv(b)); }

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

int n;
signed main() {
  while (cin >> n, n) {
    vector<int> v[35];
    set<int> s[50];
    rep(i, 0, n) s[i].insert(i);
    rep(i, 0, n) {
      int m;
      cin >> m;
      rep(j, 0, m) {
        int inp;
        cin >> inp;
        v[inp].push_back(i);
      }
    }

    int ans = INF;
    rep(i, 0, 35) {
      int sz = v[i].size();
      rep(x, 0, sz) rep(y, x + 1, sz) {
        set<int> temp = s[v[i][x]];
        s[v[i][x]].insert(s[v[i][y]].begin(), s[v[i][y]].end());
        s[v[i][y]].insert(temp.begin(), temp.end());
        if (s[v[i][x]].size() == n)
          ans = min(ans, i);
      }
    }

    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y))
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int const MOD = 1000000007;
ll mod_pow(ll x, ll n) {
  return (!n) ? 1 : (mod_pow((x * x) % MOD, n / 2) * ((n & 1) ? x : 1)) % MOD;
}
int madd(int a, int b) { return (a + b) % MOD; }
int msub(int a, int b) { return (a - b + MOD) % MOD; }
int mmul(int a, int b) { return (a * b) % MOD; }
int minv(int a) { return mod_pow(a, MOD - 2); }
int mdiv(int a, int b) { return mmul(a, minv(b)); }

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

int n, rec[100];

void solve(vector<vector<pii>> &G, int idx, int par) {
  for (auto &e : G[idx]) {
    if (e.first == par)
      continue;
    if (rec[e.first] == INF)
      solve(G, e.first, idx);
    if (rec[e.first] > e.second)
      rec[e.first] = e.second;
  }
}

signed main() {
  while (cin >> n, n) {
    vector<int> v[35];
    vector<vector<pii>> G(n);
    rep(i, 0, n) {
      int m;
      cin >> m;
      rep(j, 0, m) {
        int inp;
        cin >> inp;
        v[inp].push_back(i);
      }
    }
    rep(i, 0, 35) {
      int sz = v[i].size();
      rep(x, 0, sz) rep(y, x + 1, sz) {
        int s = v[i][x], t = v[i][y];
        G[s].push_back(pii(t, i));
        G[t].push_back(pii(s, i));
      }
    }

    rep(i, 0, n) rec[i] = INF;
    rec[0] = 1;
    solve(G, 0, -1);
    bool ng = false;
    int ans = -1;
    rep(i, 0, n) {
      if (rec[i] == INF)
        ng = true;
      ans = max(ans, rec[i]);
    }
    if (ng)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}