Problem p03911 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

class UnionFind {
private:
  vector<int> par, rank;

public:
  UnionFind(int n) {
    par.assign(n, 0);
    rank.assign(n, 0);
    for (int i = 0; i < n; ++i)
      par[i] = i;
  }

  // find root of x
  int find(int x) {
    if (par[x] == x)
      return x;
    return (par[x] = find(par[x]));
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y])
      par[x] = y;
    else {
      par[y] = x;
      if (rank[x] == rank[y])
        ++rank[x];
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  VVI lang(N);
  UnionFind uf(M);
  REP(i, N) {
    int K;
    cin >> K;
    REP(j, K) {
      int l;
      cin >> l;
      --l;
      lang[i].PB(l);
    }
    REP(j, K - 1) { uf.unite(lang[i][j], lang[i][j + 1]); }
  }

  VI sum(N);
  REP(i, N) sum[uf.find(lang[i][0])]++;

  bool ok = false;
  REP(i, M) {
    if (sum[i] == N)
      ok = true;
  }
  cout << (ok ? "YES" : "NO") << endl;

  return 0;
}


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

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

class UnionFind {
private:
  vector<int> par, rank;

public:
  UnionFind(int n) {
    par.assign(n, 0);
    rank.assign(n, 0);
    for (int i = 0; i < n; ++i)
      par[i] = i;
  }

  // find root of x
  int find(int x) {
    if (par[x] == x)
      return x;
    return (par[x] = find(par[x]));
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y])
      par[x] = y;
    else {
      par[y] = x;
      if (rank[x] == rank[y])
        ++rank[x];
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  VVI lang(N);
  UnionFind uf(M);
  REP(i, N) {
    int K;
    cin >> K;
    REP(j, K) {
      int l;
      cin >> l;
      --l;
      lang[i].PB(l);
    }
    REP(j, K - 1) { uf.unite(lang[i][j], lang[i][j + 1]); }
  }

  VI sum(M);
  REP(i, N) sum[uf.find(lang[i][0])]++;

  bool ok = false;
  REP(i, M) {
    if (sum[i] == N)
      ok = true;
  }
  cout << (ok ? "YES" : "NO") << endl;

  return 0;
}