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.
In a file maxset.py, implement the class MaxSet with the following methods:
mergemerges the sets containing the numbers a and b (if they are in different sets)get_maxreports 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
