Problem p03911 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

class UnionFind {
  vector<int> par, rank, potential;

public:
  UnionFind(int n) {
    par.resize(n, -1);
    rank.resize(n, 0);
    potential.resize(n, 0);
  }
  int root(int x) {
    if (par[x] < 0)
      return x;
    int r = root(par[x]);
    potential[x] += potential[par[x]];
    return par[x] = r;
  }
  int size(int x) { return -par[root(x)]; }
  int weight(int x) {
    root(x);
    return potential[x];
  }
  bool same(int x, int y) { return root(x) == root(y); }
  bool unite(int x, int y, int w = 0) {
    w += weight(x);
    w -= weight(y);
    x = root(x);
    y = root(y);
    if (x == y) {
      return false;
    }
    if (rank[x] < rank[y]) {
      swap(x, y);
      w = -w;
    }
    if (rank[x] == rank[y])
      rank[x]++;
    par[x] += par[y];
    par[y] = x;
    potential[y] = w;
    return true;
  }
  int diff(int x, int y) { return weight(y) - weight(x); }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> k(n);
  vector<vector<int>> l(n);
  rep(i, n) {
    cin >> k[i];
    l[i].resize(k[i]);
    rep(j, k[i]) {
      cin >> l[i][j];
      l[i][j]--;
    }
  }
  UnionFind uf(n);
  vector<int> idx(n, -1);
  rep(i, n) {
    rep(j, k[i]) {
      if (idx[l[i][j]] < 0)
        idx[l[i][j]] = i;
      else
        uf.unite(i, idx[l[i][j]]);
    }
  }
  if (uf.size(0) == n)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

class UnionFind {
  vector<int> par, rank, potential;

public:
  UnionFind(int n) {
    par.resize(n, -1);
    rank.resize(n, 0);
    potential.resize(n, 0);
  }
  int root(int x) {
    if (par[x] < 0)
      return x;
    int r = root(par[x]);
    potential[x] += potential[par[x]];
    return par[x] = r;
  }
  int size(int x) { return -par[root(x)]; }
  int weight(int x) {
    root(x);
    return potential[x];
  }
  bool same(int x, int y) { return root(x) == root(y); }
  bool unite(int x, int y, int w = 0) {
    w += weight(x);
    w -= weight(y);
    x = root(x);
    y = root(y);
    if (x == y) {
      return false;
    }
    if (rank[x] < rank[y]) {
      swap(x, y);
      w = -w;
    }
    if (rank[x] == rank[y])
      rank[x]++;
    par[x] += par[y];
    par[y] = x;
    potential[y] = w;
    return true;
  }
  int diff(int x, int y) { return weight(y) - weight(x); }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> k(n);
  vector<vector<int>> l(n);
  rep(i, n) {
    cin >> k[i];
    l[i].resize(k[i]);
    rep(j, k[i]) {
      cin >> l[i][j];
      l[i][j]--;
    }
  }
  UnionFind uf(n);
  vector<int> idx(m, -1);
  rep(i, n) {
    rep(j, k[i]) {
      if (idx[l[i][j]] < 0)
        idx[l[i][j]] = i;
      else
        uf.unite(i, idx[l[i][j]]);
    }
  }
  if (uf.size(0) == n)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}