Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
class DisjointSets:
    '''
    Implementing disjoint sets using disjoint sets forests structure.

    --- methods ---
    __init__(size): size should be the number of elements in this disjoint sets
    unite(x, y): unite sets that contain x and y into a new set
    same(x, y): check if x and y are in the same set
    '''

    def __init__(self, size):
        self.ranks = [0] * size
        self.roots = [0] * size
        for x in range(size):
            self._make_set(x)

    def _make_set(self, x):
        self.roots[x] = x
        self.ranks[x] = 0

    def _link(self, x, y):
        if self.ranks[x] > self.ranks[y]:
            self.roots[y] = x
        else:
            self.roots[x] = y
            if self.ranks[x] == self.ranks[y]:
                self.ranks[y] += 1

    def _find_set(self, x):
        # compression of the path
        if x != self.roots[x]:
            self.roots[x] = self._find_set(self.roots[x])

        return self.roots[x]

    def unite(self, x, y):
        if self.same(x, y):
            raise SameSetError('{} and {} are in the same set'.format(x, y))
        
        self._link(self._find_set(x), self._find_set(y))

    def same(self, x, y):
        return self._find_set(x) == self._find_set(y)


class SameSetError(Exception):
    pass


if __name__ == '__main__':
    import sys
    n, q = map(int, input().split())
    ds = DisjointSets(n)

    for line in sys.stdin.readlines():
        query, x, y = map(int, line.strip().split())
        if query == 0:
            ds.unite(x, y)
        elif query == 1:
            if ds.in_same_set(x, y):
                print(1)
            else:
                print(0)



Fixed submission:
# -*- coding: utf-8 -*-
class DisjointSets:
    '''
    Implementing disjoint sets using disjoint sets forests structure.

    --- methods ---
    __init__(size): size should be the number of elements in this disjoint sets
    unite(x, y): unite sets that contain x and y into a new set
    same(x, y): check if x and y are in the same set
    '''

    def __init__(self, size):
        self.size = size
        self.roots = [x for x in range(self.size)]
        self.ranks = [0] * self.size

    def _link(self, x, y):
        if self.ranks[x] > self.ranks[y]:
            self.roots[y] = x
        else:
            self.roots[x] = y
            if self.ranks[x] == self.ranks[y]:
                self.ranks[y] += 1

    def _find_set(self, x):
        # compression of the path
        if x != self.roots[x]:
            self.roots[x] = self._find_set(self.roots[x])

        return self.roots[x]

    def unite(self, x, y):
        self._link(self._find_set(x), self._find_set(y))

    def same(self, x, y):
        return self._find_set(x) == self._find_set(y)


if __name__ == '__main__':
    '''
    an example of using this function
    AIZU ONLINE JUDGE - DSL_1_A
    '''

    import sys
    n, q = map(int, input().split())
    ds = DisjointSets(n)

    for line in sys.stdin.readlines():
        query, x, y = map(int, line.strip().split())
        if query == 0:
            ds.unite(x, y)
        elif query == 1:
            if ds.same(x, y):
                print(1)
            else:
                print(0)

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
class DisjointSets:
    '''
    Implementing disjoint sets using disjoint sets forests structure.

    --- methods ---
    __init__(size): size should be the number of elements in this disjoint sets
    unite(x, y): unite sets that contain x and y into a new set
    same(x, y): check if x and y are in the same set
    '''

    def __init__(self, size):
        self.ranks = [0] * size
        self.roots = [0] * size
        for x in range(size):
            self._make_set(x)

    def _make_set(self, x):
        self.roots[x] = x
        self.ranks[x] = 0

    def _link(self, x, y):
        if self.ranks[x] > self.ranks[y]:
            self.roots[y] = x
        else:
            self.roots[x] = y
            if self.ranks[x] == self.ranks[y]:
                self.ranks[y] += 1

    def _find_set(self, x):
        # compression of the path
        if x != self.roots[x]:
            self.roots[x] = self._find_set(self.roots[x])

        return self.roots[x]

    def unite(self, x, y):
        if self.same(x, y):
            raise SameSetError('{} and {} are in the same set'.format(x, y))

        self._link(self._find_set(x), self._find_set(y))

    def same(self, x, y):
        return self._find_set(x) == self._find_set(y)


class SameSetError(Exception):
    pass


if __name__ == '__main__':
    import sys
    n, q = map(int, input().split())
    ds = DisjointSets(n)

    for line in sys.stdin.readlines():
        query, x, y = map(int, line.strip().split())
        if query == 0:
            ds.unite(x, y)
        elif query == 1:
            if ds.same(x, y):
                print(1)
            else:
                print(0)



Fixed submission:
# -*- coding: utf-8 -*-
class DisjointSets:
    '''
    Implementing disjoint sets using disjoint sets forests structure.

    --- methods ---
    __init__(size): size should be the number of elements in this disjoint sets
    unite(x, y): unite sets that contain x and y into a new set
    same(x, y): check if x and y are in the same set
    '''

    def __init__(self, size):
        self.size = size
        self.roots = [x for x in range(self.size)]
        self.ranks = [0] * self.size

    def _link(self, x, y):
        if self.ranks[x] > self.ranks[y]:
            self.roots[y] = x
        else:
            self.roots[x] = y
            if self.ranks[x] == self.ranks[y]:
                self.ranks[y] += 1

    def _find_set(self, x):
        # compression of the path
        if x != self.roots[x]:
            self.roots[x] = self._find_set(self.roots[x])

        return self.roots[x]

    def unite(self, x, y):
        self._link(self._find_set(x), self._find_set(y))

    def same(self, x, y):
        return self._find_set(x) == self._find_set(y)


if __name__ == '__main__':
    '''
    an example of using this function
    AIZU ONLINE JUDGE - DSL_1_A
    '''

    import sys
    n, q = map(int, input().split())
    ds = DisjointSets(n)

    for line in sys.stdin.readlines():
        query, x, y = map(int, line.strip().split())
        if query == 0:
            ds.unite(x, y)
        elif query == 1:
            if ds.same(x, y):
                print(1)
            else:
                print(0)

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
class DisjointSets:
    '''
    Implementing disjoint sets using disjoint sets forests structure.

    --- methods ---
    __init__(size): size should be the number of elements in this disjoint sets
    unite(x, y): unite sets that contain x and y into a new set
    same(x, y): check if x and y are in the same set
    '''

    def __init__(self, size):
        self.ranks = [0] * size
        self.roots = [0] * size
        for x in range(size):
            self._make_set(x)

    def _make_set(self, x):
        self.roots[x] = x
        self.ranks[x] = 0

    def _link(self, x, y):
        if self.ranks[x] > self.ranks[y]:
            self.roots[y] = x
        else:
            self.roots[x] = y
            if self.ranks[x] == self.ranks[y]:
                self.ranks[y] += 1

    def _find_set(self, x):
        # compression of the path
        if x != self.roots[x]:
            self.roots[x] = self._find_set(self.roots[x])

        return self.roots[x]

    def unite(self, x, y):
        if self.same(x, y):
            raise SameSetError('{} and {} are in the same set'.format(x, y))

        self._link(self._find_set(x), self._find_set(y))

    def same(self, x, y):
        return self._find_set(x) == self._find_set(y)


class SameSetError(Exception):
    pass


if __name__ == '__main__':
    import sys
    n, q = map(int, input().split())
    ds = DisjointSets(n)

    for line in sys.stdin.readlines():
        query, x, y = map(int, line.strip().split())
        if query == 0:
            ds.unite(x, y)
        elif query == 1:
            if ds.same(x, y):
                print(1)
            else:
                print(0)



Fixed submission:
# -*- coding: utf-8 -*-
class DisjointSets:
    '''
    Implementing disjoint sets using disjoint sets forests structure.

    --- methods ---
    __init__(size): size should be the number of elements in this disjoint sets
    unite(x, y): unite sets that contain x and y into a new set
    same(x, y): check if x and y are in the same set
    '''

    def __init__(self, size):
        self.size = size
        self.roots = [x for x in range(self.size)]
        self.ranks = [0] * self.size

    def _link(self, x, y):
        if self.ranks[x] > self.ranks[y]:
            self.roots[y] = x
        else:
            self.roots[x] = y
            if self.ranks[x] == self.ranks[y]:
                self.ranks[y] += 1

    def _find_set(self, x):
        # compression of the path
        if x != self.roots[x]:
            self.roots[x] = self._find_set(self.roots[x])

        return self.roots[x]

    def unite(self, x, y):
        self._link(self._find_set(x), self._find_set(y))

    def same(self, x, y):
        return self._find_set(x) == self._find_set(y)


if __name__ == '__main__':
    '''
    an example of using this function
    AIZU ONLINE JUDGE - DSL_1_A
    '''

    import sys
    n, q = map(int, input().split())
    ds = DisjointSets(n)

    for line in sys.stdin.readlines():
        query, x, y = map(int, line.strip().split())
        if query == 0:
            ds.unite(x, y)
        elif query == 1:
            if ds.same(x, y):
                print(1)
            else:
                print(0)