from typing import List





class UnionFind:

    """Union Find (Disjoint Set): O(α(N))

    References:

        https://github.com/atcoder/ac-library/blob/master/atcoder/dsu.hpp

        https://tumoiyorozu.github.io/single-file-ac-library/document_en/dsu.html

    """



    __slots__ = ["_data_size", "_roots"]



    def __init__(self, data_size: int) -> None:

        self._data_size = data_size

        self._roots = [-1] * data_size



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

        """Find the group (root) of vertex x in O(α(n)) amortized."""

        while self._roots[x] >= 0:

            x = self._roots[x]

        return x



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

        """Return whether two vertices x and y are connected in O(α(n)) amortized."""

        return self[x] == self[y]



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

        """Unite two groups of vertices x and y in O(α(n)) amortized."""

        x, y = self[x], self[y]

        if x == y:

            return

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

            x, y = y, x

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

        self._roots[y] = x





def unionfind():

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

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

    import sys



    read = sys.stdin.buffer.read



    N, Q, *TUV = list(map(int, 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__ = ["_data_size", "_roots"]



    def __init__(self, N):

        self._data_size = N

        self._roots = [-1] * N



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

        while self._roots[x] >= 0:

            x = self._roots[x]

        return x



    def unite(self, x, y):

        x = self[x]

        y = self[y]

        if x == y:

            return

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

            x, y = y, x

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

        self._roots[y] = x



    def is_connected(self, x, y):

        return self[x] == self[y]





def main():

    import sys



    read = sys.stdin.buffer.read



    N, Q, *TUV = list(map(int, 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__":

    main()

from typing import List


class UnionFind:
    """Union Find (Disjoint Set): O(α(N))
    References:
        https://github.com/atcoder/ac-library/blob/master/atcoder/dsu.hpp
        https://tumoiyorozu.github.io/single-file-ac-library/document_en/dsu.html
    """

    __slots__ = ["_data_size", "_roots"]

    def __init__(self, data_size: int) -> None:
        self._data_size = data_size
        self._roots = [-1] * data_size

    def __getitem__(self, x: int) -> int:
        """Find the group (root) of vertex x in O(α(n)) amortized."""
        while self._roots[x] >= 0:
            x = self._roots[x]
        return x

    def is_connected(self, x: int, y: int) -> bool:
        """Return whether two vertices x and y are connected in O(α(n)) amortized."""
        return self[x] == self[y]

    def unite(self, x: int, y: int) -> None:
        """Unite two groups of vertices x and y in O(α(n)) amortized."""
        x, y = self[x], self[y]
        if x == y:
            return
        if self._roots[x] > self._roots[y]:
            x, y = y, x
        self._roots[x] += self._roots[y]
        self._roots[y] = x


def unionfind():
    # https://judge.yosupo.jp/problem/unionfind
    # https://atcoder.jp/contests/practice2/tasks/practice2_a
    import sys

    read = sys.stdin.buffer.read
    N, Q, *TUV = list(map(int, 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__ = ["_data_size", "_roots"]

    def __init__(self, N):
        self._data_size = N
        self._roots = [-1] * N

    def __getitem__(self, x: int) -> int:
        while self._roots[x] >= 0:
            x = self._roots[x]
        return x

    def unite(self, x, y):
        x = self[x]
        y = self[y]
        if x == y:
            return
        elif self._roots[y] < self._roots[x]:
            x, y = y, x
        self._roots[x] += self._roots[y]
        self._roots[y] = x

    def is_connected(self, x, y):
        return self[x] == self[y]


def main():
    import sys

    read = sys.stdin.buffer.read
    N, Q, *TUV = list(map(int, 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__":
    main()

-from typing import List - - -    """Union Find (Disjoint Set): O(α(N)) -    References: -        https://github.com/atcoder/ac-library/blob/master/atcoder/dsu.hpp -        https://tumoiyorozu.github.io/single-file-ac-library/document_en/dsu.html -    """ - -    def __init__(self, data_size: int) -> None: -        self._data_size = data_size -        self._roots = [-1] * data_size +    def __init__(self, N): +        self._data_size = N +        self._roots = [-1] * N -        """Find the group (root) of vertex x in O(α(n)) amortized.""" -    def is_connected(self, x: int, y: int) -> bool: -        """Return whether two vertices x and y are connected in O(α(n)) amortized.""" -        return self[x] == self[y] - -    def unite(self, x: int, y: int) -> None: -        """Unite two groups of vertices x and y in O(α(n)) amortized.""" -        x, y = self[x], self[y] +    def unite(self, x, y): +        x = self[x] +        y = self[y] -        if self._roots[x] > self._roots[y]: +        elif self._roots[y] < self._roots[x]: +    def is_connected(self, x, y): +        return self[x] == self[y] -def unionfind(): -    # https://judge.yosupo.jp/problem/unionfind -    # https://atcoder.jp/contests/practice2/tasks/practice2_a + +def main(): -    unionfind() +    main()