Problem p03911 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;

// #define int long long
#define DEBUG 0
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define dump(o)                                                                \
  if (DEBUG) {                                                                 \
    cerr << #o << " " << o << endl;                                            \
  }
#define dumpc(o)                                                               \
  if (DEBUG) {                                                                 \
    cerr << #o;                                                                \
    for (auto &e : (o))                                                        \
      cerr << " " << e;                                                        \
    cerr << endl;                                                              \
  }
using pii = pair<int, int>;
using vi = vector<int>;
static const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
static const int MOD = (int)(1e9 + 7);

// 素集合データ構造
// Union-Find木
struct UnionFind {
  vector<int> p; // parent
  int s;         // size 集合の数
  UnionFind(int n) : p(n, -1), s(n) {}
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (size(x) < size(y))
      swap(x, y);
    p[x] += p[y];
    p[y] = x;
    s--;
    return true;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int root(int x) { return p[x] < 0 ? x : p[x] = root(p[x]); }
  // 集合の要素数
  int size(int x) { return -p[root(x)]; }
};

signed main() {
  int N, M;
  cin >> N >> M;
  UnionFind UF(N);
  vector<vector<int>> L(M);
  rep(i, 0, N) {
    int K;
    cin >> K;
    rep(j, 0, K) {
      int LL;
      cin >> LL;
      LL--;
      L[LL].emplace_back(i);
    }
  }
  rep(j, 0, M) {
    rep(k, 0, L.size()) {
      rep(l, 1, L[k].size()) { UF.unite(L[k][l], L[k][l - 1]); }
    }
  }
  cout << (UF.size(0) == N ? "YES" : "NO") << endl;
  return 0;
}

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

// #define int long long
#define DEBUG 0
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define dump(o)                                                                \
  if (DEBUG) {                                                                 \
    cerr << #o << " " << o << endl;                                            \
  }
#define dumpc(o)                                                               \
  if (DEBUG) {                                                                 \
    cerr << #o;                                                                \
    for (auto &e : (o))                                                        \
      cerr << " " << e;                                                        \
    cerr << endl;                                                              \
  }
using pii = pair<int, int>;
using vi = vector<int>;
static const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
static const int MOD = (int)(1e9 + 7);

// 素集合データ構造
// Union-Find木
struct UnionFind {
  vector<int> p; // parent
  int s;         // size 集合の数
  UnionFind(int n) : p(n, -1), s(n) {}
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (size(x) < size(y))
      swap(x, y);
    p[x] += p[y];
    p[y] = x;
    s--;
    return true;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int root(int x) { return p[x] < 0 ? x : p[x] = root(p[x]); }
  // 集合の要素数
  int size(int x) { return -p[root(x)]; }
};

signed main() {
  int N, M;
  cin >> N >> M;
  UnionFind UF(N);
  vector<vector<int>> L(M);
  rep(i, 0, N) {
    int K;
    cin >> K;
    rep(j, 0, K) {
      int LL;
      cin >> LL;
      LL--;
      L[LL].emplace_back(i);
    }
  }
  rep(k, 0, L.size()) {
    rep(l, 1, L[k].size()) { UF.unite(L[k][l], L[k][l - 1]); }
  }
  cout << (UF.size(0) == N ? "YES" : "NO") << endl;
  return 0;
}