Bitland has cities but initially no roads. Two cities belong to the same component if there is a route between them.
Implement a class that supports adding a road between two cities, and counting the number of components formed by the cities.
In a file components.py
, implement the class Components
with the following methods:
add_road
adds a road between two citiescount
returns the number of components
class Components: def __init__(self, n): # TODO def add_road(self, a, b): # TODO def count(self): # TODO if __name__ == "__main__": components = Components(5) print(components.count()) # 5 components.add_road(1, 2) components.add_road(1, 3) print(components.count()) # 3 components.add_road(2, 3) print(components.count()) # 3 components.add_road(4, 5) print(components.count()) # 2