CSES - Largest set

Initially each of the numbers 1,2,,n1,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.

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

  • merge merges the sets containing the numbers aa and bb (if they are in different sets)
  • get_max reports the size of the largest set

Both methods should be efficient.

class MaxSet:
    def __init__(self, n):
        # TODO

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

    def get_max(self):
        # TODO

if __name__ == "__main__":
    max_set = MaxSet(5)
    print(max_set.get_max()) # 1

    max_set.merge(1, 2)
    max_set.merge(3, 4)
    max_set.merge(3, 5)
    print(max_set.get_max()) # 3

    max_set.merge(1, 5)
    print(max_set.get_max()) # 5

    max_set.merge(2, 3)
    print(max_set.get_max()) # 5