class UnionFind:

    """Union-Find: O(α(N))"""



    __slots__ = ["_data_size", "_first_idx", "_parents"]



    def __init__(self, data_size: int, is_zero_origin: bool = True) -> None:

        self._data_size = data_size

        self._first_idx = 0 if is_zero_origin else 1

        self._parents = [-1] * (data_size + self._first_idx)



    def _find(self, x: int) -> int:

        if self._parents[x] < 0:

            return x

        self._parents[x] = self._find(self._parents[x])

        return self._parents[x]



    def is_connected(self, x: int, y: int) -> bool:

        """Return whether two vertices x and y are connected or not."""

        return self._find(x) == self._find(y)



    def unite(self, x: int, y: int) -> None:

        """Unite two groups of vertices x and y."""

        x, y = self._find(x), self._find(y)

        if x == y:

            return

        if self._parents[x] > self._parents[y]:

            x, y = y, x

        self._parents[x] += self._parents[y]

        self._parents[y] = x





def unionfind():

    # https://judge.yosupo.jp/problem/unionfind

    # https://atcoder.jp/contests/practice2/tasks/practice2_a

    N, Q, *TUV = list(map(int, open(0).read().split()))

    tree = UnionFind(N)

    res = []

    for t, u, v in zip(*[iter(TUV)] * 3):

        if t:

            res.append(int(tree.is_connected(u, v)))

        else:

            tree.unite(u, v)

    print(("\n".join(map(str, res))))





if __name__ == "__main__":

    unionfind()

class UnionFind:

    __slots__ = ["N", "root", "rank"]



    def __init__(self, N):

        """

        N:要素数

        root:各要素の親要素の番号を格納するリスト.

             ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数.

        rank:ランク

        """

        self.N = N

        self.root = [-1] * N



    def __repr__(self):

        return "\n".join("{}: {}".format(r, self.members(r)) for r in self.roots())



    def find(self, x):

        """頂点xの根を見つける"""

        while self.root[x] >= 0:

            x = self.root[x]

        return x



    def union(self, x, y):

        """x, yが属する木をunion"""

        # 根を比較する

        # すでに同じ木に属していた場合は何もしない.

        # 違う木に属していた場合はrankを見てくっつける方を決める.

        # rankが同じ時はrankを1増やす

        x = self.find(x)

        y = self.find(y)

        if x == y:

            return

        elif self.root[y] < self.root[x]:

            x, y = y, x

        self.root[x] += self.root[y]

        self.root[y] = x



    def same(self, x, y):

        """xとyが同じグループに属するかどうか"""

        return self.find(x) == self.find(y)



    def count(self, x):

        """頂点xが属する木のサイズを返す"""

        return -self.root[self.find(x)]



    def members(self, x):

        """xが属する木の要素を列挙"""

        _root = self.find(x)

        return [i for i in range(self.N) if self.find == _root]



    def roots(self):

        """森の根を列挙"""

        return [i for i, x in enumerate(self.root) if x < 0]



    def group_count(self):

        """連結成分の数"""

        return len(self.roots())



    def all_group_members(self):

        """{ルート要素: [そのグループに含まれる要素のリスト], ...}の辞書を返す"""

        return {r: self.members(r) for r in self.roots()}





def main():

    import sys

    input = sys.stdin.readline



    n, q = list(map(int, input().split()))

    uf = UnionFind(n)

    res = []

    for i in range(q):

        t, u, v = list(map(int, input().split()))

        if t:

            res.append(int(uf.same(u, v)))

        else:

            uf.union(u, v)

    print(("\n".join(map(str, res))))





if __name__ == "__main__":

    main()

class UnionFind:
    """Union-Find: O(α(N))"""

    __slots__ = ["_data_size", "_first_idx", "_parents"]

    def __init__(self, data_size: int, is_zero_origin: bool = True) -> None:
        self._data_size = data_size
        self._first_idx = 0 if is_zero_origin else 1
        self._parents = [-1] * (data_size + self._first_idx)

    def _find(self, x: int) -> int:
        if self._parents[x] < 0:
            return x
        self._parents[x] = self._find(self._parents[x])
        return self._parents[x]

    def is_connected(self, x: int, y: int) -> bool:
        """Return whether two vertices x and y are connected or not."""
        return self._find(x) == self._find(y)

    def unite(self, x: int, y: int) -> None:
        """Unite two groups of vertices x and y."""
        x, y = self._find(x), self._find(y)
        if x == y:
            return
        if self._parents[x] > self._parents[y]:
            x, y = y, x
        self._parents[x] += self._parents[y]
        self._parents[y] = x


def unionfind():
    # https://judge.yosupo.jp/problem/unionfind
    # https://atcoder.jp/contests/practice2/tasks/practice2_a
    N, Q, *TUV = list(map(int, open(0).read().split()))
    tree = UnionFind(N)
    res = []
    for t, u, v in zip(*[iter(TUV)] * 3):
        if t:
            res.append(int(tree.is_connected(u, v)))
        else:
            tree.unite(u, v)
    print(("\n".join(map(str, res))))


if __name__ == "__main__":
    unionfind()

class UnionFind:
    __slots__ = ["N", "root", "rank"]

    def __init__(self, N):
        """
        N:要素数
        root:各要素の親要素の番号を格納するリスト.
             ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数.
        rank:ランク
        """
        self.N = N
        self.root = [-1] * N

    def __repr__(self):
        return "\n".join("{}: {}".format(r, self.members(r)) for r in self.roots())

    def find(self, x):
        """頂点xの根を見つける"""
        while self.root[x] >= 0:
            x = self.root[x]
        return x

    def union(self, x, y):
        """x, yが属する木をunion"""
        # 根を比較する
        # すでに同じ木に属していた場合は何もしない.
        # 違う木に属していた場合はrankを見てくっつける方を決める.
        # rankが同じ時はrankを1増やす
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        elif self.root[y] < self.root[x]:
            x, y = y, x
        self.root[x] += self.root[y]
        self.root[y] = x

    def same(self, x, y):
        """xとyが同じグループに属するかどうか"""
        return self.find(x) == self.find(y)

    def count(self, x):
        """頂点xが属する木のサイズを返す"""
        return -self.root[self.find(x)]

    def members(self, x):
        """xが属する木の要素を列挙"""
        _root = self.find(x)
        return [i for i in range(self.N) if self.find == _root]

    def roots(self):
        """森の根を列挙"""
        return [i for i, x in enumerate(self.root) if x < 0]

    def group_count(self):
        """連結成分の数"""
        return len(self.roots())

    def all_group_members(self):
        """{ルート要素: [そのグループに含まれる要素のリスト], ...}の辞書を返す"""
        return {r: self.members(r) for r in self.roots()}


def main():
    import sys

    input = sys.stdin.readline
    n, q = list(map(int, input().split()))
    uf = UnionFind(n)
    res = []
    for i in range(q):
        t, u, v = list(map(int, input().split()))
        if t:
            res.append(int(uf.same(u, v)))
        else:
            uf.union(u, v)
    print(("\n".join(map(str, res))))


if __name__ == "__main__":
    main()

-    """Union-Find: O(α(N))""" +    __slots__ = ["N", "root", "rank"] -    __slots__ = ["_data_size", "_first_idx", "_parents"] +    def __init__(self, N): +        """ +        N:要素数 +        root:各要素の親要素の番号を格納するリスト. +             ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数. +        rank:ランク +        """ +        self.N = N +        self.root = [-1] * N -    def __init__(self, data_size: int, is_zero_origin: bool = True) -> None: -        self._data_size = data_size -        self._first_idx = 0 if is_zero_origin else 1 -        self._parents = [-1] * (data_size + self._first_idx) +    def __repr__(self): +        return "\n".join("{}: {}".format(r, self.members(r)) for r in self.roots()) -    def _find(self, x: int) -> int: -        if self._parents[x] < 0: -            return x -        self._parents[x] = self._find(self._parents[x]) -        return self._parents[x] +    def find(self, x): +        """頂点xの根を見つける""" +        while self.root[x] >= 0: +            x = self.root[x] +        return x -    def is_connected(self, x: int, y: int) -> bool: -        """Return whether two vertices x and y are connected or not.""" -        return self._find(x) == self._find(y) - -    def unite(self, x: int, y: int) -> None: -        """Unite two groups of vertices x and y.""" -        x, y = self._find(x), self._find(y) +    def union(self, x, y): +        """x, yが属する木をunion""" +        # 根を比較する +        # すでに同じ木に属していた場合は何もしない. +        # 違う木に属していた場合はrankを見てくっつける方を決める. +        # rankが同じ時はrankを1増やす +        x = self.find(x) +        y = self.find(y) -        if self._parents[x] > self._parents[y]: +        elif self.root[y] < self.root[x]: -        self._parents[x] += self._parents[y] -        self._parents[y] = x +        self.root[x] += self.root[y] +        self.root[y] = x + +    def same(self, x, y): +        """xとyが同じグループに属するかどうか""" +        return self.find(x) == self.find(y) + +    def count(self, x): +        """頂点xが属する木のサイズを返す""" +        return -self.root[self.find(x)] + +    def members(self, x): +        """xが属する木の要素を列挙""" +        _root = self.find(x) +        return [i for i in range(self.N) if self.find == _root] + +    def roots(self): +        """森の根を列挙""" +        return [i for i, x in enumerate(self.root) if x < 0] + +    def group_count(self): +        """連結成分の数""" +        return len(self.roots()) + +    def all_group_members(self): +        """{ルート要素: [そのグループに含まれる要素のリスト], ...}の辞書を返す""" +        return {r: self.members(r) for r in self.roots()} -def unionfind(): -    # https://judge.yosupo.jp/problem/unionfind -    # https://atcoder.jp/contests/practice2/tasks/practice2_a -    N, Q, *TUV = list(map(int, open(0).read().split())) -    tree = UnionFind(N) +def main(): +    import sys + +    input = sys.stdin.readline +    n, q = list(map(int, input().split())) +    uf = UnionFind(n) -    for t, u, v in zip(*[iter(TUV)] * 3): +    for i in range(q): +        t, u, v = list(map(int, input().split())) -            res.append(int(tree.is_connected(u, v))) +            res.append(int(uf.same(u, v))) -            tree.unite(u, v) +            uf.union(u, v) -    unionfind() +    main()