Problem p03921 (C++)

Buggy submission (status: Runtime Error):
// _______  _____  _______  _____  _______ _______ _____ _______ _____ _    _
// _______ |       |     | |  |  | |_____] |______    |      |      |      |
// \  /  |______
// |_____  |_____| |  |  | |       |______    |    __|__    |    __|__   \/
// |______
//
//  _____   ______  _____   ______  ______ _______ _______ _______ _____ __   _
//  ______   /
// |_____] |_____/ |     | |  ____ |_____/ |_____| |  |  | |  |  |   |   | \  |
// |  ____  / |       |    \_ |_____| |_____| |    \_ |     | |  |  | |  |  |
// __|__ |  \_| |_____| .
//
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define POSL(x, v)                                                             \
  (lower_bound(x.begin(), x.end(), v) - x.begin()) // ai>=v  x is sorted
#define POSU(x, v)                                                             \
  (upper_bound(x.begin(), x.end(), v) - x.begin()) // ai>v  x is sorted
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll c = gcd(a, b);
  return ((a / c) * (b / c) * c);
} // saisyo kobaisu
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;

class UnionFind {
public:
  vector<int> par;
  // 0-indexed
  void init(int n = 1) {
    par.resize(n);
    for (int i = 0; i < n; ++i)
      par[i] = i;
  }
  int root(int x = 1) {
    if (par[x] == x)
      return x;
    else {
      return par[x] = root(par[x]);
    }
  }
  void unite(int x = 1, int y = 1) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    else
      par[x] = y;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind uf;
  uf.init(N + 1);
  vector<int> lang[M + 1];
  FOR(i, 1, N + 1) {
    int k;
    cin >> k;
    FOR(j, 0, k) {
      int la;
      cin >> la;
      lang[la].push_back(i);
    }
  }
  FOR(i, 1, M + 1) {
    FOR(j, 0, lang[i].size() - 1) { uf.unite(lang[i][j], lang[i][j + 1]); }
  }
  set<int> se;
  FOR(i, 1, N + 1) { se.insert(uf.root(i)); }
  if (se.size() == 1) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}


Fixed submission:
// _______  _____  _______  _____  _______ _______ _____ _______ _____ _    _
// _______ |       |     | |  |  | |_____] |______    |      |      |      |
// \  /  |______
// |_____  |_____| |  |  | |       |______    |    __|__    |    __|__   \/
// |______
//
//  _____   ______  _____   ______  ______ _______ _______ _______ _____ __   _
//  ______   /
// |_____] |_____/ |     | |  ____ |_____/ |_____| |  |  | |  |  |   |   | \  |
// |  ____  / |       |    \_ |_____| |_____| |    \_ |     | |  |  | |  |  |
// __|__ |  \_| |_____| .
//
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define FOR(I, A, B) for (int I = int(A); I < int(B); ++I)
#define POSL(x, v)                                                             \
  (lower_bound(x.begin(), x.end(), v) - x.begin()) // ai>=v  x is sorted
#define POSU(x, v)                                                             \
  (upper_bound(x.begin(), x.end(), v) - x.begin()) // ai>v  x is sorted
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll c = gcd(a, b);
  return ((a / c) * (b / c) * c);
} // saisyo kobaisu
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;

class UnionFind {
public:
  vector<int> par;
  // 0-indexed
  void init(int n = 1) {
    par.resize(n);
    for (int i = 0; i < n; ++i)
      par[i] = i;
  }
  int root(int x = 1) {
    if (par[x] == x)
      return x;
    else {
      return par[x] = root(par[x]);
    }
  }
  void unite(int x = 1, int y = 1) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    else
      par[x] = y;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind uf;
  uf.init(N + 1);
  vector<int> lang[M + 1];
  FOR(i, 1, N + 1) {
    int k;
    cin >> k;
    FOR(j, 0, k) {
      int la;
      cin >> la;
      lang[la].push_back(i);
    }
  }
  FOR(i, 1, M + 1) {
    FOR(j, 0, lang[i].size() - 1) { uf.unite(lang[i][j], lang[i][j + 1]); }
  }
  set<int> se;
  FOR(i, 1, N + 1) { se.insert(uf.root(i)); }
  if (se.size() == 1) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}