import collections
class SatelliteNetwork:
def __init__(self):
self.connected_satellites = set()
self.adjacency_list = collections.defaultdict(list)
def on_satellite_reported_back(self, satellite_id: int):
print(f"SatelliteReportedBack: {satellite_id}")
def err_duplicate_satellite(self, satellite_id: int):
print(f"ErrDuplicateSatellite: {satellite_id}")
def err_invalid_satellite(self, satellite_id: int):
print(f"ErrInvalidSatellite: {satellite_id}")
def satellite_connected(self, satellite_id: int):
if satellite_id in self.connected_satellites:
self.err_duplicate_satellite(satellite_id)
return
self.connected_satellites.add(satellite_id)
def relationship_established(self, sat_id1: int, sat_id2: int):
error_found = False
if sat_id1 not in self.connected_satellites:
self.err_invalid_satellite(sat_id1)
error_found = True
if sat_id2 not in self.connected_satellites:
self.err_invalid_satellite(sat_id2)
error_found = True
if error_found:
return
self.adjacency_list[sat_id1].append(sat_id2)
self.adjacency_list[sat_id2].append(sat_id1)
def message_received(self, m: int, initial_satellites: list[int]):
for sat_id in initial_satellites:
if sat_id not in self.connected_satellites:
self.err_invalid_satellite(sat_id)
return
time_of_receipt = {}
bfs_queue = collections.deque()
for sat_id in set(initial_satellites):
if sat_id not in time_of_receipt:
time_of_receipt[sat_id] = 0
bfs_queue.append((sat_id, 0))
while bfs_queue:
u_id, u_time = bfs_queue.popleft()
for v_id in sorted(self.adjacency_list[u_id]):
if v_id not in time_of_receipt:
v_time = u_time + 10
time_of_receipt[v_id] = v_time
bfs_queue.append((v_id, v_time))
reports = []
for sat_id in time_of_receipt:
receipt_time_self = time_of_receipt[sat_id]
last_neighbor_receipt_time = 0
for neighbor_id in self.adjacency_list[sat_id]:
if neighbor_id in time_of_receipt:
last_neighbor_receipt_time = max(
last_neighbor_receipt_time,
time_of_receipt[neighbor_id]
)
processing_start_time = max(receipt_time_self, last_neighbor_receipt_time)
final_report_time = processing_start_time + 30
reports.append((final_report_time, sat_id))
reports.sort()
for report_time, satellite_id in reports:
self.on_satellite_reported_back(satellite_id)
if __name__ == '__main__':
network = SatelliteNetwork()
print("--- Processing Sample Case 0 ---")
# Setup for Sample Case 0
network_case0 = SatelliteNetwork()
network_case0.satellite_connected(1)
network_case0.satellite_connected(2)
network_case0.satellite_connected(3)
network_case0.relationship_established(2, 1)
network_case0.message_received(1, [2])
print("-" * 20)
print("--- Processing Custom Test Case ---")
network_case1 = SatelliteNetwork()
network_case1.satellite_connected(1)
network_case1.satellite_connected(2)
network_case1.satellite_connected(3)
network_case1.satellite_connected(4)
network_case1.satellite_connected(5)
network_case1.satellite_connected(6)
network_case1.relationship_established(1, 2)
network_case1.relationship_established(2, 3)
network_case1.relationship_established(3, 4)
network_case1.relationship_established(4, 5)
network_case1.relationship_established(5, 6)
network_case1.message_received(1, [1])
print("-" * 20)