alexjercan/bugnet huggingface dataset:
Problem p02343 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

typedef long long ll;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second

const ll mod = 1000000007;
//              123456789

using namespace std;

///////////////////////////////////////////////
//
//
///////////////////////////////////////////////

////////////////////////////////////////////////
////////////////////////////////////////////////

int g[11234];

int find(int key) {
  // cout<<key<<" "<<g[key]<<endl;
  if (g[key] < 0)
    return key;
  return g[key] = find(g[key]);
}

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

  if (g[x] < g[y])
    swap(x, y); //????????°???x??????????°???????
  g[y] += g[x];
  g[x] = y;
}

int N;
int Q;
int p;
int a, b;

int main() {

  cin >> N >> Q;

  fill(g, g + N + 1, -1);

  REP(i, Q) {
    scanf("%d %d %d", &p, &a, &b);
    if (p) {
      if (find(a) == find(b))
        puts("1");
      else {
        puts("0");
      }
    } else {
      unite(a, b);
      // cout<<g[a]<<" "<<g[b]<<"----"<<endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

typedef long long ll;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second

const ll mod = 1000000007;
//              123456789

using namespace std;

///////////////////////////////////////////////
//
//
///////////////////////////////////////////////

////////////////////////////////////////////////
////////////////////////////////////////////////

int g[11234];

int find(int key) {
  // cout<<key<<" "<<g[key]<<endl;
  if (g[key] < 0)
    return key;
  return g[key] = find(g[key]);
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (g[x] < g[y])
    swap(x, y); //????????°???x??????????°???????

  g[y] += g[x];
  g[x] = y;
}

int N;
int Q;
int p;
int a, b;

int main() {

  cin >> N >> Q;

  fill(g, g + N + 1, -1);

  REP(i, Q) {
    scanf("%d %d %d", &p, &a, &b);
    if (p) {
      if (find(a) == find(b))
        puts("1");
      else {
        puts("0");
      }
    } else {
      unite(a, b);
      // cout<<g[a]<<" "<<g[b]<<"----"<<endl;
    }
  }

  return 0;
}
Problem p02343 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int a[10005] = {};
int find(int x) {
  if (a[x] == x)
    return a[x];
  else
    return (a[x] = find(a[x]));
}
int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < 10005; i++)
    a[i] = i;
  for (int i = 0; i < q; i++) {
    int t, x, y;
    cin >> t >> x >> y;
    if (t == 0) {
      a[find(x)] = find(y);
    }
    if (t == 1) {
      cout << (find(x) == find(y) ? 1 : 0) << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a[10005] = {};
int find(int x) {
  if (a[x] == x)
    return x;
  else
    return (a[x] = find(a[x]));
}
int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < 10005; i++)
    a[i] = i;
  for (int i = 0; i < q; i++) {
    int t, x, y;
    cin >> t >> x >> y;
    if (t == 0) {
      a[find(x)] = y;
    }
    if (t == 1) {
      cout << (a[find(x)] == a[find(y)] ? 1 : 0) << endl;
    }
  }
}
Problem p02343 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
struct UnionFind {
  vector<int> par;
  UnionFind(int n) : par(n, -1) {}
  int find(int x) {
    if (par[x] < 0)
      return x;
    return par[x] = find(par[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (par[x] > par[y]) {
      par[y] += par[x];
      par[x] = y;
    } else {
      par[x] += par[y];
      par[y] = x;
    }
    return true;
  }

  bool same(int x, int y) { return find(x) == find(y); }
};
int main() {
  int n, q;
  cin >> n >> q;
  UnionFind uf(n);
  rep(i, q) {
    int c, x, y;
    cin >> c >> x >> y;
    if (c)
      cout << uf.same(x, y) << endl;
    else
      uf.unite(x, y);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
struct UnionFind {
  vector<int> par;
  UnionFind(int n) : par(n, -1) {}
  int find(int x) {
    if (par[x] < 0)
      return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (par[x] > par[y])
      swap(x, y);
    if (par[y] == par[x])
      par[x]--;
    par[y] = x;
  }

  bool same(int x, int y) { return find(x) == find(y); }
};
int main() {
  int n, q;
  cin >> n >> q;
  UnionFind uf(n);
  rep(i, q) {
    int c, x, y;
    cin >> c >> x >> y;
    if (c)
      cout << uf.same(x, y) << endl;
    else
      uf.unite(x, y);
  }
  return 0;
}
Problem p02343 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <string>

using namespace std;

map<int, int> tree;

int find(int o) {
  if (tree[o] == o) {
    return o;
  } else {
    tree[o] = find(tree[o]);
    return tree[o];
  }
}

bool check(int a, int b) { return (find(a) == find(b)); }

void unite(int a, int b) {
  if (find(a) == find(b)) {
    return;
  }
  tree[find(a)] = find(b);
}

int main() {
  int n;
  int NumberOfOperation;

  cin >> n >> NumberOfOperation;

  for (int i = 0; i < n; ++i) {
    tree[i] = i;
  }

  for (int j = 0; j < NumberOfOperation; ++j) {
    int TypeOfOperation;
    int TrialNumbers[2];

    cin >> TypeOfOperation >> TrialNumbers[0] >> TrialNumbers[1];

    if (TypeOfOperation == 0) {

      unite(TrialNumbers[0], TrialNumbers[1]);
    } else {
      cout << check(TrialNumbers[0], TrialNumbers[1]) << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <string>

using namespace std;

int main() {
  int n;
  int NumberOfOperation;

  cin >> n >> NumberOfOperation;
  map<int, int> tree;

  for (int i = 0; i < n; ++i) {
    tree[i] = i;
  }

  for (int j = 0; j < NumberOfOperation; ++j) {
    int TypeOfOperation;
    int TrialNumbers[2];

    cin >> TypeOfOperation >> TrialNumbers[0] >> TrialNumbers[1];

    if (TypeOfOperation == 0) {
      ++n;
      for (int i = 0; i < tree.size(); ++i) {

        if ((tree[i] == tree[TrialNumbers[0]] ||
             tree[i] == tree[TrialNumbers[1]]) &&
            i != TrialNumbers[0] && i != TrialNumbers[1]) {
          tree[i] = n;
        }
      }
      tree[TrialNumbers[0]] = n;
      tree[TrialNumbers[1]] = n;

    } else if (TypeOfOperation == 1) {
      if (tree[TrialNumbers[0]] == tree[TrialNumbers[1]]) {
        cout << "1" << endl;
      } else {
        cout << "0" << endl;
      }
    }
  }

  return 0;
}
Problem p02343 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#define N 10010
int n;
int father[N];
int findfather(int x) { return x == father[x] ? x : findfather(father[x]); }
void init() {
  for (int i = 0; i < n; i++)
    father[i] = i;
}
int main() {
  int m;
  scanf("%d %d", &n, &m);
  init();
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    if (a == 0) {
      father[findfather(c)] = findfather(b);
    } else {
      printf("%s\\n", findfather(b) == findfather(c) ? "1" : "0");
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
#define N 10010
int n;
int father[N];
int findfather(int x) { return x == father[x] ? x : findfather(father[x]); }
void init() {
  for (int i = 0; i < n; i++)
    father[i] = i;
}
int main() {
  int m;
  scanf("%d %d", &n, &m);
  init();
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    if (a == 0) {
      father[findfather(c)] = b;
    } else {
      printf("%s\\n", findfather(b) == findfather(c) ? "1" : "0");
    }
  }
  return 0;
}
Problem p02343 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 10010;
int fa[N];
int findfa(int x) {
  if (x == fa[x])
    return x;
  return findfa(fa[x]);
}
int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  for (int i = 0; i < n; i++)
    fa[i] = i;
  for (int time = 0; time < q; time++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);

    if (a == 1) {
      if (findfa(b) == findfa(c))
        printf("1\\n");
      else
        printf("0\\n");
    } else {
      fa[findfa(b)] = findfa(c);
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
#define N 10010
int n;
int father[N];
int findfather(int x) { return x == father[x] ? x : findfather(father[x]); }
void init() {
  for (int i = 0; i < n; i++)
    father[i] = i;
}
int main() {
  int m;
  scanf("%d %d", &n, &m);
  init();
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    if (a == 0) {
      father[findfather(c)] = b;
    } else {
      printf("%s\\n", findfather(b) == findfather(c) ? "1" : "0");
    }
  }
  return 0;
}
Problem p02343 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

static const int MAX_NUM = 10001;

int FindRoot(int elem[], int target) {
  int parent;

  parent = target;
  for (;;) {
    if (parent == elem[parent]) {
      break;
    } else {
      parent = elem[parent];
    }
  }

  return parent;
}

void Unite(int elem[], int t1, int t2) {
  int p1, p2;

  p1 = FindRoot(elem, t1);
  p2 = FindRoot(elem, t2);

  elem[p2] = p1;

  return;
}

bool Same(int elem[], int t1, int t2) {
  bool ret;
  int p1, p2;

  p1 = FindRoot(elem, t1);
  p2 = FindRoot(elem, t2);

  if (p1 == p2) {
    ret = true;
  } else {
    ret = false;
  }

  return ret;
}

int main(void) {
  int i;
  int num, qnum;
  int q;
  int t1, t2;
  int elem[MAX_NUM];

  scanf("%d %d", &num, &qnum);
  for (i = 1; i <= num; i++) {
    elem[i] = i;
  }

  for (i = 0; i < qnum; i++) {
    scanf("%d %d %d", &q, &t1, &t2);
    switch (q) {
    case 0:
      Unite(elem, t1, t2);
      break;
    case 1:
      if (Same(elem, t1, t2) == true) {
        printf("1\\n");
      } else {
        printf("0\\n");
      }
      break;
    default:
      break;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

static const int MAX_NUM = 10001;

int FindRoot(vector<int> &elem, int target) {
  int parent;

  parent = target;
  for (;;) {
    if (parent == elem[parent]) {
      break;
    } else {
      parent = elem[parent];
    }
  }

  return parent;
}

void Unite(vector<int> &elem, int t1, int t2) {
  int p1, p2;

  p1 = FindRoot(elem, t1);
  p2 = FindRoot(elem, t2);

  elem[p2] = p1;

  return;
}

bool Same(vector<int> &elem, int t1, int t2) {
  bool ret;
  int p1, p2;

  p1 = FindRoot(elem, t1);
  p2 = FindRoot(elem, t2);

  if (p1 == p2) {
    ret = true;
  } else {
    ret = false;
  }

  return ret;
}

int main(void) {
  int i;
  int num, qnum;
  int q;
  int t1, t2;
  vector<int> elem;

  scanf("%d %d", &num, &qnum);
  elem.resize(num, 0);
  for (i = 1; i <= num; i++) {
    elem[i] = i;
  }

  for (i = 0; i < qnum; i++) {
    scanf("%d %d %d", &q, &t1, &t2);
    switch (q) {
    case 0:
      Unite(elem, t1, t2);
      break;
    case 1:
      if (Same(elem, t1, t2) == true) {
        printf("1\\n");
      } else {
        printf("0\\n");
      }
      break;
    default:
      break;
    }
  }
  return 0;
}
Problem p02343 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
using namespace std;
const int num = 5000;
vector<int> v[num];
int cnt = -1;
inline void merge(int a, int b) {
  int m = -1, n = -1;
  for (int i = 0; i <= cnt; i++) {
    for (int j = 0; j < v[i].size(); j++) {
      if (m != -1 && n != -1)
        break;
      if (v[i][j] == a)
        m = i;
      if (v[i][j] == b)
        n = i;
    }
    if (m != -1 && n != -1)
      break;
  }
  if (m != -1 && n != -1 && m != n) {
    int x = v[n].size();
    for (int i = 0; i < x; i++) {
      v[m].push_back(v[n][0]);
      v[n].erase(v[n].begin());
    }
  } else if (m != -1 && n != -1 && m == n)
    return;
  else if (m != -1 && n == -1)
    v[m].push_back(b);
  else if (m == -1 && n != -1)
    v[n].push_back(a);
  else {
    cnt++;
    v[cnt].push_back(a);
    v[cnt].push_back(b);
  }
}
inline void same(int a, int b) {
  int m = -1, n = -1;
  for (int i = 0; i <= cnt; i++) {
    for (int j = 0; j < v[i].size(); j++) {
      if (m != -1 && n != -1)
        break;
      if (v[i][j] == a)
        m = i;
      if (v[i][j] == b)
        n = i;
      if (m != -1 && n != -1)
        break;
    }
  }
  if (m != -1 && n != -1 && m == n)
    cout << '1' << endl;
  else
    cout << '0' << endl;
}

int main() {
  int n, q, com, fi, se;
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    cin >> com >> fi >> se;
    if (com == 0)
      merge(fi, se);
    else
      same(fi, se);
  }
  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
using namespace std;
const int num = 5000;
vector<int> v[num];
int cnt = -1;
inline void merge(int a, int b) {
  int m = -1, n = -1;
  for (int i = 0; i <= cnt; i++) {
    for (int j = 0; j < v[i].size(); j++) {
      if (m != -1 && n != -1)
        break;
      if (v[i][j] == a)
        m = i;
      if (v[i][j] == b)
        n = i;
    }
    if (m != -1 && n != -1)
      break;
  }
  if (m != -1 && n != -1 && m != n) {
    int x = v[n].size();
    for (int i = 0; i < x; i++) {
      v[m].push_back(v[n][i]);
      v[n].erase(v[n].begin() + i);
    }
  } else if (m != -1 && n != -1 && m == n)
    return;
  else if (m != -1 && n == -1)
    v[m].push_back(b);
  else if (m == -1 && n != -1)
    v[n].push_back(a);
  else {
    cnt++;
    v[cnt].push_back(a);
    v[cnt].push_back(b);
  }
}
inline void same(int a, int b) {
  int m = -1, n = -1;
  for (int i = 0; i <= cnt; i++) {
    for (int j = 0; j < v[i].size(); j++) {
      if (m != -1 && n != -1)
        break;
      if (v[i][j] == a)
        m = i;
      if (v[i][j] == b)
        n = i;
      if (m != -1 && n != -1)
        break;
    }
  }
  if (m != -1 && n != -1 && m == n)
    cout << '1' << endl;
  else
    cout << '0' << endl;
}

int main() {
  int n, q, com, fi, se;
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    cin >> com >> fi >> se;
    if (com == 0)
      merge(fi, se);
    else
      same(fi, se);
  }
  return 0;
}
Problem p02343 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

struct edge {
  int dest;
  int weight; // = dest minus curr
};

class UF {
public:
  vector<edge> v;
  UF(int n) { v = vector<edge>(n, {-1, 0}); }
  void unite(int x, int y, int y_minus_x) {
    int loc_x = find(x);
    int loc_y = find(y);
    if (loc_x == loc_y)
      return;
    if (v[loc_x].dest <
        v[loc_y].dest) { // x's set has more elements -- join y to x
      v[loc_x].dest += v[loc_y].dest;
      v[loc_y] = {loc_x, -y_minus_x};
    } else {
      v[loc_y].dest += v[loc_x].dest;
      v[loc_x] = {loc_y, y_minus_x};
    }
  }
  void unite(int x, int y) { unite(x, y, 0); }

  // if weighted = true, returns the difference of the root and the current node
  // if weighted = false, returns the id of the root
  int find(int x, bool weighted = false) {
    int tmp = 0;
    while (v[x].dest >= 0) {
      tmp += v[x].weight;
      x = v[x].dest;
    }
    return (weighted ? tmp : x);
  }
  // pre: x and y are in the same set
  // post: returns y - x
  int get_difference(int x, int y) {
    int root_minus_x = find(x, true);
    int root_minus_y = find(y, true);
    return root_minus_x - root_minus_y;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  UF uf(n);
  int c, x, y;
  while (cin >> c >> x >> y) {
    if (c == 0)
      uf.unite(x, y);
    if (c == 1)
      cout << (uf.find(x) == uf.find(y)) << endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

struct edge {
  int dest;
  int weight; // = dest minus curr
};

class UF {
public:
  vector<edge> v;
  UF(int n) { v = vector<edge>(n, {-1, 0}); }
  void unite(int x, int y, int y_minus_x) {
    int loc_x = find(x);
    int loc_y = find(y);
    if (v[loc_x].dest <
        v[loc_y].dest) { // x's set has more elements -- join y to x
      v[loc_x].dest += v[loc_y].dest;
      v[loc_y] = {loc_x, -y_minus_x};
    } else {
      v[loc_y].dest += v[loc_x].dest;
      v[loc_x] = {loc_y, y_minus_x};
    }
  }
  void unite(int x, int y) { unite(x, y, 0); }

  // if weighted = true, returns the difference of the root and the current node
  // if weighted = false, returns the id of the root
  int find(int x, bool weighted = false) {
    int tmp = 0;
    while (v[x].dest >= 0) {
      tmp += v[x].weight;
      x = v[x].dest;
    }
    return (weighted ? tmp : x);
  }
  // pre: x and y are in the same set
  // post: returns y - x
  int get_difference(int x, int y) {
    int root_minus_x = find(x, true);
    int root_minus_y = find(y, true);
    return root_minus_x - root_minus_y;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  UF uf(n);
  int c, x, y;
  while (cin >> c >> x >> y) {
    if (c == 0)
      uf.unite(x, y);
    if (c == 1)
      cout << (uf.find(x) == uf.find(y)) << endl;
  }
}
Problem p02343 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<int> rank;

  int n, q;
  cin >> n >> q;

  for (int i = 0; i < n; i++) {
    rank.push_back(i);
  }

  for (int i = 0; i < q; i++) {
    int c;
    int x, y;

    cin >> c >> x >> y;
    if (c == 0) {
      int a = rank[x];
      int b = rank[y];

      if (a == b) {
        continue;
      }
      for (int j = 0; j < n; j++) {
        if (rank[j] == b)
          rank[j] = a;
      }
    } else {
      if (rank[x] == rank[y]) {
        cout << 1 << endl;
      } else {
        cout << 0 << endl;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<int> rank;

  int n, q;
  cin >> n >> q;

  for (int i = 0; i < n; i++) {
    rank.push_back(i);
  }

  for (int i = 0; i < q; q++) {
    int c;
    int x, y;

    cin >> c >> x >> y;
    if (c) {
      int a = rank[x];
      int b = rank[y];

      if (a == b) {
        continue;
      }
      for (int j = 0; j < n; j++) {
        if (rank[j] == b)
          rank[j] = a;
      }
    } else {
      if (rank[x] == rank[y]) {
        cout << 1 << endl;
      } else {
        cout << 0 << endl;
      }
    }
  }
}
Problem p02343 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

class Node {
public:
  const int value;
  Node *prev;
  Node *next;

  Node(int value) : value(value) {}
  ~Node() {
    delete prev;
    delete next;
  }
};

class LinkedList {
  Node *guard;
  int _size;

public:
  const int id;

  LinkedList(int id, Node *first) : id(id) {
    guard = new Node(-1);
    guard->next = first;
    guard->prev = first;
    first->next = guard;
    first->prev = guard;
    _size = 1;
  }

  ~LinkedList() { delete guard; }

  void append_all(LinkedList *other) {
    last()->next = other->first();
    other->first()->prev = last();

    guard->prev = other->last();
    other->last()->next = guard;

    _size += other->size();
  }

  Node *first() { return guard->next; }

  Node *last() { return guard->prev; }

  bool is_end(Node *node) { return node == guard; }

  int size() { return _size; }
};

LinkedList **SETs;

void unite(int x, int y) {
  if (SETs[x] == SETs[y])
    return;

  bool is_x_bigger = SETs[x]->size() >= SETs[y]->size();
  LinkedList *main = is_x_bigger ? SETs[x] : SETs[y];
  LinkedList *sub = is_x_bigger ? SETs[y] : SETs[x];

  Node *node = sub->first();
  while (!sub->is_end(node)) {
    SETs[node->value] = main;
    node = node->next;
  }
  main->append_all(sub);
}

bool same(int x, int y) { return SETs[x]->id == SETs[y]->id; }

void create_sets(int n) {
  for (int i = 0; i < n; i++) {
    Node *node = new Node(i);
    LinkedList *set = new LinkedList(i, node);
    SETs[i] = set;
  }
}

int main() {
  int n, q;
  std::cin >> n >> q;

  SETs = new LinkedList *[n];
  create_sets(n);

  int command, x, y;
  for (int i = 0; i < q; i++) {
    std::cin >> command >> x >> y;
    switch (command) {
    case 0:
      unite(x, y);
      break;
    case 1:
      std::cout << (same(x, y) ? 1 : 0) << "\\n";
      break;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

/* DSL1_A: Disjoint Set */

#include <iostream>
#include <set>
using std::set;

set<int> *sets;
int *ids;

void unite(int x, int y) {
  x = ids[x];
  y = ids[y];
  if (x == y)
    return;

  int main, sub;
  if (sets[x].size() < sets[y].size()) {
    main = y;
    sub = x;
  } else {
    main = x;
    sub = y;
  }

  set<int> subset = sets[sub];
  set<int>::iterator it;
  for (it = subset.begin(); it != subset.end(); it++) {
    int member = *it;
    sets[main].insert(member);
    ids[member] = main;
  }
}

bool same(int x, int y) {
  set<int> s = sets[ids[x]];
  return s.find(y) != s.end();
}

int main() {
  int n, q;
  std::cin >> n >> q;

  sets = new set<int>[n];
  ids = new int[n];
  for (int i = 0; i < n; i++) {
    sets[i].insert(i);
    ids[i] = i;
  }

  int command, x, y;
  for (int i = 0; i < q; i++) {
    std::cin >> command >> x >> y;
    switch (command) {
    case 0:
      unite(x, y);
      break;
    case 1:
      std::cout << (same(x, y) ? 1 : 0) << "\\n";
      break;
    }
  }
  return 0;
}