alexjercan/bugnet huggingface dataset:
Problem p02343 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define INT_MAX 2147483647
#define INT_MIN -2147483646
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for (int i = (int)n; i >= 1; i--)
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
void fastios() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

static const int N = 10005;
vi p(N, -1);
vi d(N, 1);

void unite(int x, int y) {
  while (p[x] != -1) {
    x = p[x];
  }
  while (p[y] != -1) {
    y = p[y];
  }
  if (x == y)
    return;
  if (d[x] > d[y])
    p[y] = x;
  else if (d[x] < d[y])
    p[x] = y;
  else {
    p[y] = x;
    d[x]++;
  }
}

bool same(int x, int y) {
  while (p[x] != -1) {
    x = p[x];
  }
  while (p[y] != -1) {
    y = p[y];
  }
  if (x == y)
    return 1;
  else
    return 0;
}

int main() {
  fastios();
  int n, q;
  cin >> n >> q;
  Loop(i, q) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0)
      unite(x, y);
    else
      cout << same(x, y) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define INT_MAX 2147483647
#define INT_MIN -2147483646
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for (int i = (int)n; i >= 1; i--)
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
void fastios() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

static const int N = 10005;
vi p(N, -1);
vi d(N, 1);

void unite(int x, int y) {
  while (p[x] != -1) {
    x = p[x];
  }
  while (p[y] != -1) {
    y = p[y];
  }
  if (d[x] > d[y])
    p[y] = x;
  else if (d[x] < d[y])
    p[x] = y;
  else {
    p[y] = x;
    d[x]++;
  }
}

bool same(int x, int y) {
  while (p[x] != -1) {
    x = p[x];
  }
  while (p[y] != -1) {
    y = p[y];
  }
  if (x == y)
    return 1;
  else
    return 0;
}

int main() {
  fastios();
  int n, q;
  cin >> n >> q;
  Loop(i, q) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0)
      unite(x, y);
    else
      cout << same(x, y) << endl;
  }
  return 0;
}
Problem p02343 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>

#define N_MAX 10000
#define Q_MAX 100000
// #define NIL -1
#define UNITE 0
#define SAME 1
using namespace std;

int N, Q;
int num_array[N_MAX + 1]; // 0オリジン．

int main() {
  scanf("%d %d", &N, &Q);
  for (int i = 0; i < N_MAX; i++)
    num_array[i] = i; // iはグループ番号

  // クエリごとの処理
  int query;
  int x, y;
  int p1, p2;
  int cnt1, cnt2;
  for (int q = 0; q < Q; q++) {
    scanf("%d %d %d", &query, &x, &y);

    p1 = x;
    p2 = y;
    cnt1 = 0;
    cnt2 = 0;
    while (num_array[p1] != p1) {
      p1 = num_array[p1];
      cnt1++;
    }
    while (num_array[p2] != p2) {
      p2 = num_array[p2];
      cnt2++;
    }

    if (query == UNITE) {
      if (cnt1 > cnt2) {
        num_array[p1] = p2;
      } else {
        num_array[p2] = p1;
      }

    } else if (query == SAME) {
      if (p1 == p2)
        printf("1\\n");
      else
        printf("0\\n");
    } else {
      cout << "error" << endl;
      return 0;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

#define N_MAX 10000
#define Q_MAX 100000
// #define NIL -1
#define UNITE 0
#define SAME 1
using namespace std;

int N, Q;
int num_array[2 * N_MAX + 1]; // 0オリジン．

int main() {
  scanf("%d %d", &N, &Q);
  for (int i = 0; i < 2 * N_MAX; i++)
    num_array[i] = i; // iはグループ番号
  // for(int i = 0;i < N;i++)
  //	num_array[i] = i;//iはグループ番号

  // クエリごとの処理
  int query;
  int x, y;
  int p1, p2;
  int cnt = N;
  for (int q = 0; q < Q; q++) {
    scanf("%d %d %d", &query, &x, &y);

    p1 = x;
    p2 = y;
    while (num_array[p1] != p1)
      p1 = num_array[p1];
    while (num_array[p2] != p2)
      p2 = num_array[p2];

    if (query == UNITE) {
      num_array[p1] = cnt;
      num_array[p2] = cnt;
      cnt++;

    } else if (query == SAME) {
      if (p1 == p2)
        printf("1\\n");
      else
        printf("0\\n");
    } else {
      cout << "error" << endl;
      return 0;
    }
  }

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

#include <algorithm>
#include <array>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

typedef pair<int, int> pii;

using namespace std;

struct UnionFind {
  vector<int> parent;
  vector<int> rank;

  UnionFind(int n) : parent(n), rank(n, 0) {
    for (int i = 0; i < n; i++) {
      parent[i] = i;
    }
  }

  int find(int x) {
    if (x == parent[x])
      return x;
    else
      return parent[x] = find(parent[x]);
  }

  bool same(int x, int y) { return find(x) == find(y); }

  void unite(int x, int y) {
    int a = find(x);
    int b = find(y);

    if (a == b) {
      return;
    }

    if (rank[a] < rank[b]) {
      parent[a] = b;
    } else {
      parent[b] = a;
      if (rank[a] == rank[b])
        rank[b]++;
    }
  }
};

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

  UnionFind uf(n);

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

    switch (c) {
    case 0:
      uf.unite(x, y);
      break;
    case 1:
      cout << (uf.same(x, y) ? 1 : 0) << endl;
      break;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

typedef pair<int, int> pii;

using namespace std;

struct UnionFind {
  vector<int> parent;
  vector<int> rank;

  UnionFind(int n) : parent(n), rank(n, 0) {
    for (int i = 0; i < n; i++) {
      parent[i] = i;
    }
  }

  int find(int x) {
    if (x == parent[x])
      return x;
    else
      return parent[x] = find(parent[x]);
  }

  bool same(int x, int y) { return find(x) == find(y); }

  void unite(int x, int y) {
    int a = parent[x];
    int b = parent[y];

    if (a == b) {
      return;
    }

    if (rank[a] < rank[b]) {
      parent[a] = b;
    } else {
      parent[b] = a;
      if (rank[a] == rank[b])
        rank[b]++;
    }
  }
};

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

  UnionFind uf(n);

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

    switch (c) {
    case 0:
      uf.unite(x, y);
      break;
    case 1:
      cout << (uf.same(x, y) ? 1 : 0) << endl;
      break;
    }
  }

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

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

class UnionFind {
public:
  vector<int> parents;

  UnionFind(int n) : parents(n) {
    for (int i = 0; i < n; i++) {
      parents[i] = i;
    }
  }

  int root(int x) {
    if (parents[x] == x) { // root
      return x;
    } else {
      return parents[x] = root(parents[x]);
    }
  }

  bool isSame(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;

    parents[x] = y;
  }
};

int main(void) {
  int n, q;
  cin >> n >> q;
  UnionFind uf(n);

  for (int i = 0; i < q; i++) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0) {
      uf.unite(x, y);
    } else {
      cout << (uf.isSame(x, y) ? 1 : 0) << endl;
    }
  }

  return 0;
}


Error: -6
double free or corruption (out)


Fixed submission:

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

class UnionFind {
public:
  vector<int> parents;

  UnionFind(int n) : parents(n) {
    for (int i = 0; i < n; i++) {
      parents[i] = i;
    }
  }

  int root(int x) {
    if (parents[x] == x) { // root
      return x;
    } else {
      return parents[x] = root(parents[x]);
    }
  }

  bool isSame(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;

    parents[x] = y;
  }
};

int main(void) {
  int n, q;
  cin >> n >> q;
  UnionFind uf(n);

  for (int i = 0; i < q; i++) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0) {
      uf.unite(x - 1, y - 1);
    } else {
      cout << (uf.isSame(x - 1, y - 1) ? 1 : 0) << endl;
    }
  }

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

#if 0

#endif

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

int P[10000 + 10];

void init(int N) {
  for (int i = 0; i <= N; ++i)
    P[i] = i;
}
int root(int a) {
  if (P[a] == a)
    return a;
  else
    return (P[a] = root(P[a]));
}
bool is_same_set(int a, int b) { return root(a) == root(b); }
void unite(int a, int b) { P[root(a)] = root(b); }

int main() {
  int n, q;
  int com, x, y;
  cin >> n >> q;
  init(n);
  for (int i = 0; i < q; ++i) {
    cin >> com >> x >> y;
    if (com == 0)
      unite(x, y);
    else {
      if (is_same_set(x, y))
        cout << "1" << endl;
      else if (!is_same_set(x, y))
        cout << "0" << endl;
    }
  }
}

Error: -11

Fixed submission:

#if 0

#endif

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

int P[10000 + 10];

void init(int N) {
  for (int i = 0; i <= N; ++i)
    P[i] = i;
}
int root(int a) {
  if (P[a] == a)
    return a;
  else
    return (P[a] = root(P[a]));
}
bool is_same_set(int a, int b) { return root(a) == root(b); }
void unite(int a, int b) { P[root(a)] = root(b); }

int main() {
  int n, q;
  vector<int> com, x, y;
  cin >> n >> q;
  init(n);
  for (int i = 0; i < q; ++i) {
    cin >> com[i] >> x[i] >> y[i];
    if (com[i] == 0)
      unite(x[i], y[i]);
    else if (is_same_set(x[i], y[i]))
      cout << 1 << endl;
    else if (!is_same_set(x[i], y[i]))
      cout << 0 << endl;
  }
}
Problem p02343 (C++) — buggy submission, verdict: Runtime Error

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

void makeset(int n, vector<list<int>> &set) {
  set.resize(n);
  for (int i = 0; i < n; i++) {
    set[i].push_back(i);
  }
}

void unite(vector<list<int>> &set, int x, int y, int n) {
  list<int>::iterator itr;
  int idx1 = -1, idx2 = -1;
  for (int i = 0; i < n; i++) {
    if (set[i].empty())
      continue;
    for (itr = set[i].begin(); itr != set[i].end(); itr++) {
      if (*itr == x)
        idx1 = i;
      if (*itr == y)
        idx2 = i;
    }
    if (idx1 != -1 && idx2 != -1)
      break;
  }
  if (idx1 == idx2)
    return;
  set[idx1].merge(set[idx2]);
  set[idx2].clear();
}

void same(vector<list<int>> &set, int x, int y, int n) {
  list<int>::iterator itr;
  int idx1 = -1, idx2 = -1;
  for (int i = 0; i < n; i++) {
    if (set[i].empty())
      continue;
    for (itr = set[i].begin(); itr != set[i].end(); itr++) {
      if (*itr == x)
        idx1 = i;
      if (*itr == y)
        idx2 = i;
    }
    if (idx1 != -1 && idx2 != -1)
      break;
  }
  if (idx1 == idx2)
    cout << '1' << endl;
  else
    cout << '0' << endl;
}

int main(void) {
  int n, q, x, y, c;
  cin >> n >> q;
  vector<list<int>> set;
  makeset(n, set);
  for (int i = 0; i < q; i++) {
    cin >> c >> x >> y;
    if (c == 0)
      unite(set, x, y, n);
    else
      same(set, x, y, n);
  }
}


Error: 0

Fixed submission:

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

void makeset(int n, vector<list<int>> &set) {
  set.resize(n);
  for (int i = 0; i < n; i++) {
    set[i].push_back(i);
  }
}

void unite(vector<list<int>> &set, int x, int y, int n) {
  list<int>::iterator itr;
  int idx1 = -1, idx2 = -1;
  for (int i = 0; i < n; i++) {
    if (set[i].empty())
      continue;
    for (itr = set[i].begin(); itr != set[i].end(); itr++) {
      if (*itr == x)
        idx1 = i;
      if (*itr == y)
        idx2 = i;
    }
    if (idx1 != -1 && idx2 != -1)
      break;
  }
  set[idx1].merge(set[idx2]);
  set[idx2].clear();
}

void same(vector<list<int>> &set, int x, int y, int n) {
  list<int>::iterator itr;
  int idx1 = -1, idx2 = -1;
  for (int i = 0; i < n; i++) {
    if (set[i].empty())
      continue;
    for (itr = set[i].begin(); itr != set[i].end(); itr++) {
      if (*itr == x)
        idx1 = i;
      if (*itr == y)
        idx2 = i;
    }
    if (idx1 != -1 && idx2 != -1)
      break;
  }
  if (idx1 == idx2)
    cout << '1' << endl;
  else
    cout << '0' << endl;
}

int main(void) {
  int n, q, x, y, c;
  cin >> n >> q;
  vector<list<int>> set;
  makeset(n, set);
  for (int i = 0; i < q; i++) {
    cin >> c >> x >> y;
    if (c == 0)
      unite(set, x, y, n);
    else
      same(set, x, y, n);
  }
}
Problem p02343 (C++) — buggy submission, verdict: Runtime Error

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

int root(int i, vector<int> &parent) {
  if (parent[i] == -1) {
    return i;
  } else {
    int result = root(parent[i], parent);
    parent[i] = result; // パス圧縮
    return result;
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> parent(n, -1); //-1が親なしを示す
  for (int i = 0; i < q; ++i) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0) {
      // unite
      if (root(x, parent) != root(y, parent)) {
        parent[root(y, parent)] = root(x, parent);
      }
    } else {
      // same
      if (root(x, parent) == root(y, parent)) {
        cout << 1 << endl;
      } else {
        cout << 0 << endl;
      }
    }
  }
}


Error: 0

Fixed submission:

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

int root(int i, vector<int> &set) {
  if (set[i] == -1) {
    return i;
  } else {
    int result = root(set[i], set);
    set[i] = result;
    return result;
  }
}
int main() {
  int n, q;
  cin >> n >> q;
  vector<int> set(n, -1);
  for (int i = 0; i < q; ++i) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0) {
      // unite
      set[root(y, set)] = root(x, set);
    } else {
      // same
      if (root(x, set) == root(y, set)) {
        cout << 1 << endl;
      } else {
        cout << 0 << endl;
      }
    }
  }
}
Problem p02343 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <vector>

typedef struct node {
  int number;
  node *parent;
} node;

node *root;

node *find(node *x) {

  if (x->parent == root)
    return x;
  else {
    x->parent = find(x->parent);
    return x->parent;
  }
}

int main(void) {
  int n, q;
  scanf("%d %d", &n, &q);
  std::vector<node> nodes(n);
  for (int i = 0; i < n; i++) {
    nodes[i].number = i;
    nodes[i].parent = root;
  }
  for (int i = 0; i < q; i++) {
    int op, x, y;
    node *x_parent, *y_parent;
    scanf("%d %d %d", &op, &x, &y);
    x_parent = find(&nodes[x]);
    y_parent = find(&nodes[y]);
    if (op == 0) {
      if (x_parent->number > y_parent->number)
        x_parent->parent = y_parent;
      else if (x_parent->number < y_parent->number)
        y_parent->parent = x_parent;
    } else {
      if (x_parent->number == y_parent->number)
        printf("1\\n");
      else
        printf("0\\n");
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <vector>

typedef struct node {
  int number;
  node *parent;
} node;

node *root;

node *find(node *x) {

  if (x->parent == root)
    return x;
  else {
    x->parent = find(x->parent);
    return x->parent;
  }
}

int main(void) {
  int n, q;
  scanf("%d %d", &n, &q);
  std::vector<node> nodes(n);
  for (int i = 0; i < n; i++) {
    nodes[i].number = i;
    nodes[i].parent = root;
  }
  for (int i = 0; i < q; i++) {
    int op, x, y;
    node *x_parent, *y_parent;
    scanf("%d %d %d", &op, &x, &y);
    x_parent = find(&nodes[x]);
    y_parent = find(&nodes[y]);
    if (op == 0) {
      if (x_parent->number > y_parent->number)
        x_parent->parent = y_parent;
      else
        y_parent->parent = x_parent;
    } else {
      if (x_parent->number == y_parent->number)
        printf("1\\n");
      else
        printf("0\\n");
    }
  }
  return 0;
}
Problem p02343 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int p[100010], r[100010];
void makeset(int x) {
  for (int i = 0; i < x; ++i) {
    p[i] = i;
    r[i] = 0;
  }
}
int findset(int a) {
  if (a != p[a])
    p[a] = findset(p[a]);
  return p[a];
}
bool same(int a, int b) { return (findset(a) == findset(b)); }

void link(int a, int b) {
  if (r[a] > r[b]) {
    p[b] = a;
  } else {
    p[a] = b;
    if (r[a] == r[b]) {
      r[b]++;
    }
  }
}
void unite(int a, int b) { link(findset(a), findset(b)); }
int main() {
  int n, q;
  cin >> n >> q;
  makeset(n);
  int x[100001], y[100001], comp[100001];
  for (int i = 0; i < q; ++i) {
    cin >> comp[i] >> x[i] >> y[i];
    if (comp[i] == 0) {
      unite(x[i], y[i]);
    } else if (comp[i] == 1) {
      int t = same(x[i], y[i]);
      cout << t << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int p[100010], r[100010];
void makeset(int x) {
  for (int i = 0; i < x; ++i) {
    p[i] = i;
    r[i] = 0;
  }
}
int findset(int a) {
  if (a != p[a])
    p[a] = findset(p[a]);
  return p[a];
}
bool same(int a, int b) { return (findset(a) == findset(b)); }

void link(int a, int b) {
  if (r[a] > r[b]) {
    p[b] = a;
  } else {
    p[a] = b;
    if (r[a] == r[b]) {
      r[b]++;
    }
  }
}
void unite(int a, int b) { link(findset(a), findset(b)); }
int main() {
  int n;
  long int q;
  cin >> n >> q;
  makeset(n);
  int x[10010], y[10010], comp[10010];
  for (long int i = 0; i < q; ++i) {
    cin >> comp[i] >> x[i] >> y[i];
    if (comp[i] == 0) {
      unite(x[i], y[i]);
    } else if (comp[i] == 1) {
      int t = same(x[i], y[i]);
      cout << t << endl;
    }
  }
}
Problem p02343 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;
#define DEBUG(x) cout << #x << ": " << x << endl

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define INT(a) int((a) + 1e-9)
int DX[4] = {1, 0, -1, 0};
int DY[4] = {0, 1, 0, -1};

typedef pair<int, int> Pii;

#define SUPnl 10002

int nl;
int p[SUPnl];

int init() {
  REP(ni, nl) { p[ni] = -1; }
}

int getRoot(int a) {
  while (true) {
    if (p[a] == -1)
      return a;
    a = p[a];
  }
}

bool same(int a, int b) {
  int ra = getRoot(a);
  int rb = getRoot(b);
  if (ra != a)
    p[a] = ra;
  if (rb != b)
    p[b] = rb;
  return ra == rb;
}

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

int main() {
  int ol;
  cin >> nl >> ol;
  init();
  int ot, a, b;
  REP(oi, ol) {
    cin >> ot >> a >> b;
    if (ot == 0)
      unite(a, b);
    if (ot == 1)
      cout << same(a, b) << endl;
  }
}

Error: TLE

Fixed submission:

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

using namespace std;
#define DEBUG(x) cout << #x << ": " << x << endl

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define INT(a) int((a) + 1e-9)
int DX[4] = {1, 0, -1, 0};
int DY[4] = {0, 1, 0, -1};

typedef pair<int, int> Pii;

#define SUPnl 10002

int nl;
int p[SUPnl];

int init() {
  REP(ni, nl) { p[ni] = -1; }
}

int getRoot(int a) {
  while (true) {
    if (p[a] == -1)
      return a;
    a = p[a];
  }
}

bool same(int a, int b) {
  int pa = getRoot(a);
  int pb = getRoot(b);
  if (pa == pb) {
    p[b] = pa;
    return true;
  } else
    return false;
}

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

int main() {
  int ol;
  cin >> nl >> ol;
  init();
  int ot, a, b;
  REP(oi, ol) {
    cin >> ot >> a >> b;
    if (ot == 0)
      unite(a, b);
    if (ot == 1)
      cout << same(a, b) << endl;
  }
}
Problem p02343 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

struct UF {
  vector<int> a;
  vector<int> size;

  UF(int n) {
    a.assign(n, 0);
    size.assign(n, 1);
    for (int i = 0; i < n; i++) {
      a[i] = i;
    }
  }

  int root(int u) {
    int parent = u;
    while (a[parent] != parent) {
      parent = a[parent];
    }
    return parent;
  }

  bool same(int u, int v) { return root(u) == root(v); }

  void unite(int u, int v) {
    int i = root(u);
    int j = root(v);
    if (i != j) {
      if (size[i] > size[j]) {
        a[j] = i;
        size[i] += size[j];
      } else {
        a[i] = j;
        size[j] += size[i];
      }
    }
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  UF uf = UF(n + 1);
  for (int i = 0; i < q; i++) {
    int cmd, u, v;
    cin >> cmd >> u >> v;
    if (cmd == 0) {
      uf.unite(u, v);
    } else {
      cout << (uf.same(u, v) ? 1 : 0) << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

struct UF {
  vector<int> a;
  vector<int> size;

  UF(int n) {
    a.assign(n, 0);
    size.assign(n, 1);
    for (int i = 0; i < n; i++) {
      a[i] = i;
    }
  }

  int root(int u) {
    int parent = u;
    while (a[parent] != parent) {
      parent = a[u];
    }
    return parent;
  }

  bool same(int u, int v) { return root(u) == root(v); }

  void unite(int u, int v) {
    int i = root(u);
    int j = root(v);
    if (i != j) {
      if (size[i] > size[j]) {
        a[j] = i;
        size[i] += size[j];
      } else {
        a[i] = j;
        size[j] += size[i];
      }
    }
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  UF uf = UF(n + 1);
  for (int i = 0; i < q; i++) {
    int cmd, u, v;
    cin >> cmd >> u >> v;
    if (cmd == 0) {
      uf.unite(u, v);
    } else {
      cout << (uf.same(u, v) ? 1 : 0) << endl;
    }
  }
  return 0;
}