Problem p03921 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <tuple>
#include <vector>
using namespace std;

#define FOR(i, init, a) for (int i = init; i < a; i++)
#define rep(i, a) FOR(i, 0, a)
#define rrep(i, a) for (int i = a; i >= 0; i--)
#define rep1(i, a) for (int i = 1; i <= a; i++)
#define cout1(a) cout << a << endl;
#define cout2(a, b) cout << a << " " << b << endl;
#define cout3(a, b, c) cout << a << " " << b << " " << c << endl;
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;
#define mem(a, n) memset(a, n, sizeof(a))
#define all(a) a.begin(), a.end()

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LLINF = 1e18;
static const double pi = 3.141592653589793;

int rt = INF;

class UnionFind {
private:
  int n;
  vector<int> uni;

public:
  UnionFind(int _n) {
    n = _n;
    uni.clear();
    uni.resize(
        n,
        -1); // 値が負であれば根であり、かつ-1*値は頂点の数。値が正であればそれが親を表す。
  }
  int root(int x) {
    if (uni[x] < 0)
      return x;
    return uni[x] = root(uni[x]);
  }
  bool same(int x, int y) { return root(x) == root(y); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (x > y)
      swap(x, y);
    uni[x] += uni[y];
    uni[y] = x;
    if (rt > x)
      rt = x;
  }
  int getSize(int x) { return -uni[root(x)]; }
  void print() {
    for (auto x : uni)
      cout << x << " ";
    cout << endl;
  }
};

int N, M;
bool used[100009];
UnionFind un(100009);

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

  cin >> N >> M;
  rep1(a, N) {
    int K, l;
    cin >> K;
    rep(i, K) {
      cin >> l;
      un.unite(a, N + l);
    }
  }

  rep1(a, N) {
    if (!un.same(1, a)) {
      cout1("NO");
      return 0;
    }
  }
  cout1("YES");
}

Fixed submission:
#include "bits/stdc++.h"
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <tuple>
#include <vector>
using namespace std;

#define FOR(i, init, a) for (int i = init; i < a; i++)
#define rep(i, a) FOR(i, 0, a)
#define rrep(i, a) for (int i = a; i >= 0; i--)
#define rep1(i, a) for (int i = 1; i <= a; i++)
#define cout1(a) cout << a << endl;
#define cout2(a, b) cout << a << " " << b << endl;
#define cout3(a, b, c) cout << a << " " << b << " " << c << endl;
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;
#define mem(a, n) memset(a, n, sizeof(a))
#define all(a) a.begin(), a.end()

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LLINF = 1e18;
static const double pi = 3.141592653589793;

int rt = INF;

class UnionFind {
private:
  int n;
  vector<int> uni;

public:
  UnionFind(int _n) {
    n = _n;
    uni.clear();
    uni.resize(
        n,
        -1); // 値が負であれば根であり、かつ-1*値は頂点の数。値が正であればそれが親を表す。
  }
  int root(int x) {
    if (uni[x] < 0)
      return x;
    return uni[x] = root(uni[x]);
  }
  bool same(int x, int y) { return root(x) == root(y); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (x > y)
      swap(x, y);
    uni[x] += uni[y];
    uni[y] = x;
    if (rt > x)
      rt = x;
  }
  int getSize(int x) { return -uni[root(x)]; }
  void print() {
    for (auto x : uni)
      cout << x << " ";
    cout << endl;
  }
};

int N, M;
UnionFind un(200009);

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

  cin >> N >> M;
  rep1(a, N) {
    int K, l;
    cin >> K;
    rep(i, K) {
      cin >> l;
      un.unite(a, N + l);
    }
  }

  rep1(a, N) {
    if (!un.same(1, a)) {
      cout1("NO");
      return 0;
    }
  }
  cout1("YES");
}