CSES - Largest set

Initially each of the numbers 1,2,\dots,n is in its own set. Your task is to implement a class that supports merging two sets and finding the largest set.

You may assume that n is at most 10^5 and that the methods of the class are called at most 10^5 times.

In a file maxset.py, implement a class MaxSet with the following methods:

  • constructor with n as a parameter
  • merge merges the sets containing a and b (if they are in different sets)
  • get_max reports the size of the largest set
class MaxSet:
    def __init__(self,n):
        # TODO

    def merge(self,a,b):
        # TODO

    def get_max(self):
        # TODO

if __name__ == "__main__":
    m = MaxSet(5)
    print(m.get_max()) # 1
    m.merge(1,2)
    m.merge(3,4)
    m.merge(3,5)
    print(m.get_max()) # 3
    m.merge(1,5)
    print(m.get_max()) # 5