usrbinenv python3 0based indexing is used in this n is given as an input which is the number of elements in the set unionfind data structure along with required functions takes n integer as input which is the total number of nodes in the structure function used to find root of a node x input node number integer between 0 and n1 output node number integer between 0 and n1 function to perform union of the nodes a and b input node number node number integers between 0 and n1 output none function to check if a and b are connected input node number node number integers between 0 and n1 output true or false usr bin env python3 0 based indexing is used in this n is given as an input which is the number of elements in the set union find data structure along with required functions takes n integer as input which is the total number of nodes in the structure function used to find root of a node x input node number integer between 0 and n 1 output node number integer between 0 and n 1 function to perform union of the nodes a and b input node number node number integers between 0 and n 1 output none function to check if a and b are connected input node number node number integers between 0 and n 1 output true or false
class Union_find:
    
    def __init__ (self, n):
        self.ar = [None] * n
        for i in range(n):
            self.ar[i] = i
        self.size = [1] * n
        self.n = n

    def root (self, x):
        
        while self.ar[x] != x:
            self.ar[x] = self.ar[self.ar[x]]
            x = self.ar[x]
        return x

    def union (self, a, b):
        
        print("Union", a, "and", b)
        roa = self.root(a)
        rob = self.root(b)
        if self.size[roa] < self.size[rob]:
            self.ar[roa] = self.ar[rob]
            self.size[rob] += self.size[roa]
        else:
            self.ar[rob] = self.ar[roa]
            self.size[roa] += self.size[rob]

    def find (self, a, b):
        
        if self.root (a) == self.root (b):
            return True
        else:
            return False

    pass

if __name__ == "__main__":
    uf = Union_find (6)
    uf.union (1, 2)
    print("2 and 3 connected?", uf.find(2, 3))
    uf.union (1, 3)
    uf.union (4, 5)
    print("2 and 4 connected?", uf.find(2, 4))
    uf.union (3, 5)
    print("2 and 4 connected?", uf.find(2, 4))