The Time When the Network Becomes Idle - Problem

There is a network of n servers, labeled from 0 to n - 1. You are given a 2D integer array edges, where edges[i] = [ui, vi] indicates there is a message channel between servers ui and vi, and they can pass any number of messages to each other directly in one second.

You are also given a 0-indexed integer array patience of length n.

All servers are connected, i.e., a message can be passed from one server to any other server(s) directly or indirectly through the message channels.

The server labeled 0 is the master server. The rest are data servers. Each data server needs to send its message to the master server for processing and wait for a reply. Messages move between servers optimally, so every message takes the least amount of time to arrive at the master server. The master server will process all newly arrived messages instantly and send a reply to the originating server via the reversed path the message had gone through.

At the beginning of second 0, each data server sends its message to be processed. Starting from second 1, at the beginning of every second, each data server will check if it has received a reply to the message it sent (including any newly arrived replies) from the master server:

  • If it has not, it will resend the message periodically. The data server i will resend the message every patience[i] second(s), i.e., the data server i will resend the message if patience[i] second(s) have elapsed since the last time the message was sent from this server.
  • Otherwise, no more resending will occur from this server.

The network becomes idle when there are no messages passing between servers or arriving at servers.

Return the earliest second starting from which the network becomes idle.

Input & Output

Example 1 — Basic Network
$ Input: edges = [[0,1],[1,2]], patience = [0,2,1]
Output: 8
💡 Note: Server 1 (distance 1): round trip = 2, patience = 2, no resending needed, idle at time 2+1 = 3. Server 2 (distance 2): round trip = 4, patience = 1, resends at times 0,1,2,3, last send at 3, idle at time 3+4 = 7+1 = 8.
Example 2 — No Resending
$ Input: edges = [[0,1],[0,2],[1,2]], patience = [0,10,10]
Output: 3
💡 Note: Both servers 1 and 2 have distance 1, round trip = 2. Since patience ≥ round trip, no resending occurs. Network idle at time 2+1 = 3.
Example 3 — Multiple Resends
$ Input: edges = [[0,1],[1,2],[2,3]], patience = [0,1,1,1]
Output: 8
💡 Note: Server 3 has distance 3, round trip = 6, patience = 1. Resends every second until reply arrives. Last send at time 5, idle at time 5+6 = 11, but other servers finish earlier, so answer is calculated from the maximum.

Constraints

  • n == patience.length
  • 2 ≤ n ≤ 105
  • patience[0] == 0
  • 1 ≤ patience[i] ≤ 105 for 1 ≤ i < n
  • 1 ≤ edges.length ≤ min(105, n × (n - 1) / 2)
  • edges[i].length == 2
  • 0 ≤ ui, vi < n
  • ui != vi
  • There are no duplicate edges
  • Each server can directly or indirectly reach every other server

Visualization

Tap to expand
Network Message Processing: Find When Network Becomes IdleMaster(0)12Message Timeline• Each server sends at time 0• Resends based on patience value• Master processes instantly• Replies follow reverse path• Idle when all trips completeInput: edges=[[0,1],[1,2]], patience=[0,2,1] → Output: 8Network idle when last message completes its round trip
Understanding the Visualization
1
Network Setup
Master server (0) connected to data servers via edges
2
Message Flow
Each data server sends messages and waits for replies
3
Idle Calculation
Network becomes idle when all messages complete round trips
Key Takeaway
🎯 Key Insight: Calculate mathematically when each server's last message completes its round trip, rather than simulating every second
Asked in
Google 15 Microsoft 12
18.0K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen