1
# Runtime: 2578 ms (Top 92.45%) | Memory: 69.3 MB (Top 19.78%)3
def networkBecomesIdle(self, edges: List[List[int]], patience: List[int]) -> int:6
adjList = defaultdict(list)8
for source, target in edges:9
adjList[source].append(target)10
adjList[target].append(source)12
# BFS to get the shortest route from node to master.14
queue = deque([(0, 0)])17
currPos, currDist = queue.popleft()22
shortest[currPos] = currDist24
for nei in adjList[currPos]:25
queue.append((nei, currDist + 1))27
# Calculate answer using shortest paths.29
for index in range(1, len(patience)):30
resendInterval = patience[index]32
# The server will stop sending requests after it's been sent to the master node and back.33
shutOffTime = shortest[index] * 235
# shutOffTime-1 == Last second the server can send a re-request.36
lastSecond = shutOffTime - 138
# Calculate the last time a packet is actually resent.39
lastResentTime = (lastSecond // resendInterval) * resendInterval41
# At the last resent time, the packet still must go through 2 more cycles to the master node and back.42
lastPacketTime = lastResentTime + shutOffTime44
ans = max(lastPacketTime, ans)46
# Add +1, the current answer is the last time the packet is recieved by the target server (still active).47
# We must return the first second the network is idle, therefore + 1