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

#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

class Disjointset {
private:
  vector<int> p, rank;

public:
  Disjointset(int size) {
    p.resize(size, 0);
    rank.resize(size, 0);
    for (int i = 0; i < size; ++i) {
      p[i] = i;
    }
  }
  int find(int x) {
    if (p[x] != x)
      p[x] = find(p[x]);
    return p[x];
  }
  bool isSame(int x, int y) { return find(x) == find(y); }
  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      if (x == y)
        ++rank[y];
      p[x] = y;
    }
  }
  void unite(int x, int y) { link(find(x), find(y)); }
};

int main() {

  int n, q;
  scanf("%d %d", &n, &q);

  Disjointset d(n);

  for (int i = 0; i < q; ++i) {
    int com, x, y;
    scanf("%d %d %d", &com, &x, &y);
    if (com == 0) {
      d.unite(x, y);
    } else {
      if (d.isSame(x, y)) {
        cout << 1 << endl;
      } else {
        cout << 0 << endl;
      }
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

class Disjointset {
private:
  vector<int> p, rank;

public:
  Disjointset(int size) {
    p.resize(size, 0);
    rank.resize(size, 0);
    for (int i = 0; i < size; ++i) {
      p[i] = i;
    }
  }
  int find(int x) {
    if (p[x] != x)
      p[x] = find(p[x]);
    return p[x];
  }
  bool isSame(int x, int y) { return find(x) == find(y); }
  void link(int x, int y) {
    if (x > y) {
      p[y] = x;
    } else {
      if (x == y)
        ++y;
      p[x] = y;
    }
  }
  void unite(int x, int y) { link(find(x), find(y)); }
};

int main() {

  int n, q;
  scanf("%d %d", &n, &q);

  Disjointset d(n);

  for (int i = 0; i < q; ++i) {
    int com, x, y;
    scanf("%d %d %d", &com, &x, &y);
    if (com == 0) {
      d.unite(x, y);
    } else {
      if (d.isSame(x, y)) {
        cout << 1 << endl;
      } else {
        cout << 0 << endl;
      }
    }
  }

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

#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

#define N_MAX 10000
#define NIL -1

struct Node {
  int value;
  int parent;
};

struct Node node[N_MAX];

void init(int n) {
  for (int i = 0; i <= n; i++) {
    node[i].value = i;
    node[i].parent = NIL;
  }
}

int find(int x) {
  struct Node target_node = node[x];
  vector<int> v;
  while (target_node.parent != NIL) {
    v.push_back(target_node.value);
    target_node = node[target_node.parent];
  }

  for (int i = 0; i < v.size(); i++) {
    node[v[i]].parent = target_node.value;
  }

  return target_node.value;
}

void unite(int x, int y) {
  if (find(x) != find(y)) {
    node[find(x)].parent = y;
  }
}

void same(int x, int y) {
  if (find(x) == find(y)) {
    printf("1\\n");
  } else {
    printf("0\\n");
  }
}

int main(void) {
  int n, q;
  scanf("%d", &n);
  scanf("%d", &q);

  init(n);

  for (int i = 0; i < q; i++) {
    int com, x, y;
    scanf("%d", &com);
    scanf("%d", &x);
    scanf("%d", &y);

    if (com == 0) {
      unite(x, y);
    } else {
      same(x, y);
    }
  }
}


Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

#define N_MAX 10000
#define NIL -1

struct Node {
  int value;
  int parent;
};

struct Node node[N_MAX];

void init(int n) {
  for (int i = 0; i < n; i++) {
    node[i].value = i;
    node[i].parent = NIL;
  }
}

int find(int x) {
  struct Node target_node = node[x];
  vector<int> v;
  while (target_node.parent != NIL) {
    v.push_back(target_node.value);
    target_node = node[target_node.parent];
  }

  for (int i = 0; i < v.size(); i++) {
    node[v[i]].parent = target_node.value;
  }

  return target_node.value;
}

void unite(int x, int y) { node[find(x)].parent = y; }

void same(int x, int y) {
  if (find(x) == find(y)) {
    printf("1\\n");
  } else {
    printf("0\\n");
  }
}

int main(void) {
  int n, q;
  scanf("%d", &n);
  scanf("%d", &q);

  init(n);

  for (int i = 0; i < q; i++) {
    int com, x, y;
    scanf("%d", &com);
    scanf("%d", &x);
    scanf("%d", &y);

    if (com == 0) {
      unite(x, y);
    } else {
      same(x, y);
    }
  }
}
Problem p02343 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

class UnionFind {
private:
  vector<int> data;

public:
  UnionFind(const int &N) : data(vector<int>(N, -1)) {}

  int root(const int &x) {
    if (data[x] < 0) {
      return x;
    } else {
      return data[x] = root(data[x]);
    }
  }

  int size(const int &x) { return -data[root(x)]; }

  bool same(const int &x, const int &y) { return root(x) == root(y); }

  void unite(int x, int y) {
    if (same(x, y))
      return;
    x = root(x);
    y = root(y);
    if (size(x) < size(y)) {
      data[y] += data[x];
      data[x] = y;
    } else {
      data[x] += data[y];
      data[y] = x;
    }
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  UnionFind UF(n);
  for (size_t i = 0; i < q; i++) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0) {
      UF.unite(x, y);
    }
    if (com == 1) {
      cout << UF.same(x, y) << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, q, com, x, y;
  cin >> n >> q;
  vector<vector<int>> vertexs(n);
  vector<int> ans;
  for (int i = 0; i < q; i++) {
    cin >> com >> x >> y;
    if (com) {
      queue<int> que;
      vector<bool> DP(n, 1);
      DP[x] = 0;
      que.push(x);
      while (que.size()) {
        for (int j = 0; j < vertexs[que.front()].size(); j++) {
          if (DP[vertexs[que.front()][j]])
            que.push(vertexs[que.front()][j]);
          DP[vertexs[que.front()][j]] = 0;
          vertexs[x].push_back(vertexs[que.front()][j]);
          vertexs[vertexs[que.front()][j]].push_back(x);
          if (vertexs[que.front()][j] == y)
            break;
        }
        que.pop();
      }
      if (DP[y])
        ans.push_back(0);
      else
        ans.push_back(1);
    } else {
      vertexs[x].push_back(y);
      vertexs[y].push_back(x);
    }
  }
  for (int i = 0; i < ans.size(); i++)
    cout << ans[i] << endl;
}
Problem p02343 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

class UnionFind {
private:
  vector<int> v;

public:
  UnionFind(int n) {
    for (int i = 0; i < n; i++)
      v.emplace_back(-1);
  }
  int root(int x) { return v[x] < 0 ? x : v[x] = root(v[x]); }
  void unite(int x, int y) {
    if (root(x) == root(y)) {
      return;
    }
    v[root(x)] = root(y);
  }
  bool same(int x, int y) { return root(x) == root(y); }
};

int main() {
  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) {
      cout << uf.same(x, y) << endl;
    } else {
      uf.unite(x, y);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)

using namespace std;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
};

int main() {
  long long int n, q;
  while (cin >> n >> q, n || q) {
    int a[100002];
    a[1] = 1;
    for (long long int i = 2; i <= n; i++) {
      cin >> a[i];
    }
    vector<bool> roots(n);
    roots[1] = true;
    vector<pair<char, long long int>> queries;
    for (long long int i = 0; i < q; i++) {
      char c;
      long long int v;
      cin >> c >> v;
      if (c == 'M' && roots[v])
        continue;
      if (c == 'M')
        roots[v] = true;
      queries.emplace_back(make_pair(c, v));
    }
    UnionFind uf(n + 2);
    for (long long int i = 2; i <= n; i++) {
      if (!roots[i]) {
        // cout << a[i] << " - " << i << endl;
        uf.unionSet(a[i], i);
      }
    }
    long long int sum = 0;
    for (long long int i = (long long int)queries.size() - 1; i >= 0; i--) {
      // cout << queries[i].first << " " << queries[i].second << endl;
      if (queries[i].first == 'M') {
        uf.unionSet(a[queries[i].second], queries[i].second);
      }
      if (queries[i].first == 'Q') {
        // cout << uf.find(queries[i].second) << endl;
        sum += uf.root(queries[i].second);
      }
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p02343 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

int s[10001] = {0};
int n, q;

int top(int x) {
  if (s[x] == x) {
    return x;
  }
  return top(s[x]);
}
int len(int x, int l = 0) {
  if (s[x] == x) {
    return l;
  }
  return len(s[x], l + 1);
}

int uni(int x, int y) {
  int lx = len(x);
  int ly = len(y);
  int tx = top(x);
  int ty = top(y);
  if (lx < ly) {
    s[ty] = tx;
  } else {
    s[tx] = ty;
  }
  return 0;
}

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

int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i < n; i++) {
    s[i] = i;
  }
  int com, x, y;
  for (int I = 0; I < q; I++) {
    scanf("%d%d%d", &com, &x, &y);
    if (com == 0) {
      uni(x, y);
    } else {
      printf("%d\\n", same(x, y) ? 1 : 0);
    }
  }
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#define FOR(i, min, max) for (int(i) = (min); (i) < (max); (i)++)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define SWAP(a, b)                                                             \\
  {                                                                            \\
    int r = a;                                                                 \\
    a = b;                                                                     \\
    b = r;                                                                     \\
  }

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> s[10000];
  FOR(i, 0, n)
  s[i].push_back(i);
  int q;
  cin >> q;
  FOR(ite, 0, q) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0) {
      int rem = -1;
      FOR(i, 0, n) {
        FOR(j, 0, s[i].size()) {
          if (s[i][j] == x) {
            if (rem < 0)
              rem = i;
            else {
              if (i != rem) {
                FOR(k, 0, s[rem].size())
                s[i].push_back(s[rem][k]);
                s[n - 1].swap(s[rem]);
              }
            }
          } else if (s[i][j] == y) {
            if (rem < 0)
              rem = i;
            else {
              if (i != rem) {
                FOR(k, 0, s[rem].size())
                s[i].push_back(s[rem][k]);
                s[n - 1].swap(s[rem]);
              }
            }
          }
        }
      }
      n--;
    } else {
      bool flag = false;
      FOR(i, 0, n) {
        FOR(j, 0, s[i].size()) {
          if (s[i][j] == x) {
            FOR(k, 0, s[i].size()) {
              if (s[i][k] == y) {
                flag = true;
                break;
              }
            }
          } else if (s[i][j] == y) {
            FOR(k, 0, s[i].size()) {
              if (s[i][k] == x) {
                flag = true;
                break;
              }
            }
          }
          if (flag)
            break;
        }
        if (flag)
          break;
      }
      if (flag)
        cout << 1 << endl;
      else
        cout << 0 << endl;
    }
  }
  return 0;
}
Problem p02343 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>

using namespace std;

template <int SIZE> class DisjointSetsForests {
  int p[SIZE];
  int rank[SIZE];

public:
  void MakeSet(int x) {
    p[x] = x;
    rank[x] = 0;
  }
  void Union(int x, int y) { Link(FindSet(x), FindSet(y)); }
  int FindSet(int x) {
    if (x != p[x])
      p[x] = FindSet(p[x]);
    return p[x];
  }
  bool IsSameSet(int x, int y) { return FindSet(x) == FindSet(y); }
  void Link(int x, int y) {
    if (rank[x] > rank[y])
      p[y] = x;
    else {
      p[x] = y;
      if (rank[x] == rank[y])
        rank[y]++;
    }
  }
};
typedef DisjointSetsForests<10000> UnionFind;

int main() {
  int n, q;
  cin >> n >> q;
  UnionFind uf;
  for (int i = 0; i < n; i++) {
    uf.MakeSet(i);
  }
  for (int i = 0; i < q; i++) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0) {
      uf.Union(x, y);
    } else {
      if (uf.IsSameSet(x, y)) {
        cout << 1 << endl;
      } else {
        cout << 0 << endl;
      }
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <set>

using namespace std;

void showset(set<int> &s) {
  for (auto it = s.begin(); it != s.end(); it++) {
    cout << *it << ", ";
  }
  cout << endl;
}

typedef set<int> setint;

int main() {
  setint *sets[10000];
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    sets[i] = new setint();
    sets[i]->insert(i);
  }
  for (int i = 0; i < q; i++) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0) {
      sets[x]->insert(sets[y]->begin(), sets[y]->end());
      sets[y] = sets[x];
      for (auto it = sets[x]->begin(); it != sets[x]->end(); it++) {
        if (*it != x && *it != y) {
          sets[*it] = sets[y];
        }
      }
    } else {
      if (sets[x]->find(y) == sets[x]->end()) {
        cout << 0 << endl;
      } else {
        cout << 1 << endl;
      }
    }
  }
  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: -11

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);
  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: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>

template <class T> using vec = std::vector<T>;

constexpr int nil = -1;

class DisjointSet {
public:
  DisjointSet(int n) : rank(n, 0), p(n, nil) {}

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

    int rx = root(x);
    int ry = root(y);

    if (rank[rx] == rank[ry]) {
      p[rx] = ry;
      ++rank[ry];
    } else if (rank[rx] < rank[ry]) {
      p[rx] = ry;
    } else {
      p[ry] = rx;
    }
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == nil)
      return false;
    return rx == ry;
  }

  int root(int x) {
    int i = x;
    while (true) {
      if (p[i] == nil)
        break;
      i = p[i];
    }
    return i;
  }

private:
  vec<int> rank, p;
};

void solve() {
  using namespace std;

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

  DisjointSet ds{n};

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

    if (com == 0)
      ds.unite(x, y);
    else
      cout << (ds.same(x, y) ? 1 : 0) << endl;
  }
}

int main() {
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

template <class T> using vec = std::vector<T>;

constexpr int nil = -1;

class DisjointSet {
public:
  DisjointSet(int n) : rank(n, 0), p(n, nil) {}

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);

    if (rank[rx] == rank[ry]) {
      p[rx] = ry;
      ++rank[ry];
    } else if (rank[rx] < rank[ry]) {
      p[rx] = ry;
    } else {
      p[ry] = rx;
    }
  }

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

  int root(int x) {
    while (true) {
      if (p[x] == nil)
        break;
      x = p[x];
    }
    return x;
  }

private:
  vec<int> rank, p;
};

void solve() {
  using namespace std;

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

  DisjointSet ds{n};

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

    if (com == 0)
      ds.unite(x, y);
    else
      cout << (ds.same(x, y) ? 1 : 0) << endl;
  }
}

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

#include <algorithm>
#include <cstdio>
#include <unordered_map>
using namespace std;
int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  unordered_map<int, int> v;
  for (int i = 0; i < n; i++)
    v[i] = i;
  for (int i = 0; i < q; i++) {
    int com, x, y;
    scanf("%d %d %d", &com, &x, &y);
    for (;;)
      if (x == v[x])
        break;
      else
        x = v[x];
    for (;;)
      if (y == v[y])
        break;
      else
        y = v[y];
    if (com) {
      printf("%d\\n", v[x] == v[y] ? 1 : 0);
    } else {
      if (x > y)
        swap(x, y);
      if (v[x] > v[y])
        v[x] = v[y];
      else {
        int tmp = x;
        for (;;) {
          if (tmp == v[tmp]) {
            v[y] = tmp;
            break;
          } else {
            tmp = v[tmp];
          }
        }
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <unordered_map>
using namespace std;
int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  unordered_map<int, int> v;
  for (int i = 0; i < n; i++)
    v[i] = i;
  for (int i = 0; i < q; i++) {
    int com, x, y;
    scanf("%d %d %d", &com, &x, &y);
    if (com) {
      printf("%d\\n", v[x] == v[y] ? 1 : 0);
    } else {
      int tmp = v[y];
      for (int i = 0; i < n; i++)
        if (v[i] == tmp)
          v[i] = v[x];
    }
  }
}
Problem p02343 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
static const int MAX = 200000;

void trace(int data[], int n) {
  cout << data[0];
  for (int i = 1; i < n; i++) {
    cout << " " << data[i];
  }
  cout << endl;
}

class Dtree {
public:
  vector<int> rank, parent;

  Dtree(int size) {
    rank.resize(size, 0);
    parent.resize(size, 0);
    for (int i = 0; i < parent.size(); i++)
      parent[i] = i;
    for (int i = 0; i < rank.size(); i++)
      rank[i] = 0;
  }
  int unite(int x, int y) {
    while (1) {
      int pre = parent[y];
      parent[y] = label(x);
      if (y == pre)
        break;
      y = pre;
    }
  }
  int same(int x, int y) { return label(x) == label(y); }
  int label(int x) {
    while (x != parent[x]) {
      x = parent[x];
    }
    return x;
  }
};

int main() {
  int data[MAX];
  int n, nquery;
  cin >> n;
  cin >> nquery;
  Dtree dt = Dtree(n);
  for (int i = 0; i < nquery; i++) {
    int x, y, command;
    cin >> command >> x >> y;
    if (command == 0) {
      dt.unite(x, y);
    } else if (command == 1) {
      cout << (dt.same(x, y) ? 1 : 0) << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
static const int MAX = 200000;

void trace(int data[], int n) {
  cout << data[0];
  for (int i = 1; i < n; i++) {
    cout << " " << data[i];
  }
  cout << endl;
}

class Dtree {
public:
  vector<int> rank, parent;

  Dtree(int size) {
    rank.resize(size, 0);
    parent.resize(size, 0);
    for (int i = 0; i < parent.size(); i++)
      parent[i] = i;
    for (int i = 0; i < rank.size(); i++)
      rank[i] = 0;
  }
  int unite(int x, int y) {
    while (y != x) {
      int pre = parent[y];
      parent[y] = label(x);
      y = parent[pre];
    }
  }
  int same(int x, int y) { return label(x) == label(y); }
  int label(int x) {
    while (x != parent[x]) {
      x = parent[x];
    }
    return x;
  }
};

int main() {
  int data[MAX];
  int n, nquery;
  cin >> n;
  cin >> nquery;
  Dtree dt = Dtree(n);
  for (int i = 0; i < nquery; i++) {
    int x, y, command;
    cin >> command >> x >> y;
    if (command == 0) { // unite
      dt.unite(x, y);
    } else if (command == 1) { // same
      cout << (dt.same(x, y) ? 1 : 0) << endl;
    }
  }
}