Problem p03921 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (i = 0; i < n; i++)
#define loop(i, a, n) for (i = a; i < n; i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

typedef vector<int> vi;
typedef pair<int, int> pii;

#define MAX_N 100000

int parent[MAX_N];
int ran[MAX_N];

void init(int n) {
  int i;
  rep(i, n) {
    parent[i] = i;
    ran[i] = 0;
  }
}
int find(int x) {
  if (parent[x] == x)
    return x;
  else
    return parent[x] = find(parent[x]);
}

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

  if (ran[x] < ran[y])
    parent[x] = y;
  else {
    parent[y] = x;
    if (ran[x] == ran[y])
      ran[x]++;
  }
}

vi lan[1005];

int main() {
  int i, j;
  int n, m;
  cin >> n >> m;
  //  vi lan(m);
  rep(i, n) {
    int k, l;
    cin >> k;
    rep(j, k) {
      cin >> l;
      l--;                 // 0indexed
      lan[l].push_back(i); // 0indexed
    }
  }

  init(n);
  rep(i, m) { rep(j, lan[i].size()) unite(lan[i][0], lan[i][j]); }
  bool ans = true;
  rep(i, n) {
    if (find(parent[0]) != find(parent[i]))
      ans = false;
  }
  if (ans)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (i = 0; i < n; i++)
#define loop(i, a, n) for (i = a; i < n; i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

typedef vector<int> vi;
typedef pair<int, int> pii;

#define MAX_N 100000

int parent[MAX_N];
int ran[MAX_N];

void init(int n) {
  int i;
  rep(i, n) {
    parent[i] = i;
    ran[i] = 0;
  }
}
int find(int x) {
  if (parent[x] == x)
    return x;
  else
    return parent[x] = find(parent[x]);
}

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

  if (ran[x] < ran[y])
    parent[x] = y;
  else {
    parent[y] = x;
    if (ran[x] == ran[y])
      ran[x]++;
  }
}

vi lan[100005];

int main() {
  int i, j;
  int n, m;
  cin >> n >> m;
  //  vi lan(m);
  rep(i, n) {
    int k, l;
    cin >> k;
    rep(j, k) {
      cin >> l;
      l--;                 // 0indexed
      lan[l].push_back(i); // 0indexed
    }
  }

  init(n);
  rep(i, m) { rep(j, lan[i].size()) unite(lan[i][0], lan[i][j]); }
  bool ans = true;
  rep(i, n) {
    if (find(parent[0]) != find(parent[i]))
      ans = false;
  }
  if (ans)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}