The Time When the Network Becomes Idle

medium graph bfs

Problem

A network of n servers (node 0 is the master) is connected by bidirectional links, each taking one second to traverse. At second 0 every non-master server sends a message to the master. A server with patience patience[i] resends its message every patience[i] seconds until it receives the reply, which arrives a round-trip time after the message that triggered it. Return the earliest second when no more messages are travelling anywhere (the network becomes idle).

The round-trip time of server i is 2 · dist[i], where dist[i] is its shortest hop distance from the master — found with a single BFS. From that we compute, per server, when its last (needless) resend happens and when its final reply lands; the network is idle one second after the latest such reply.

Inputedges = [[0,1],[1,2],[0,2]], patience = [0,2,1]
Output4
Both servers are 1 hop away (round-trip 2). Server 2 resends at second 1, and that reply returns at second 3, so the network is idle at second 4.

from collections import deque

def network_becomes_idle(edges, patience):
    n = len(patience)
    adj = {i: [] for i in range(n)}
    for u, v in edges:
        adj[u].append(v)
        adj[v].append(u)
    dist = [-1] * n
    dist[0] = 0
    q = deque([0])
    while q:
        u = q.popleft()
        for v in adj[u]:
            if dist[v] == -1:
                dist[v] = dist[u] + 1
                q.append(v)
    ans = 0
    for i in range(1, n):
        rtt = 2 * dist[i]
        last_send = (rtt - 1) // patience[i] * patience[i]
        last_reply = last_send + rtt
        ans = max(ans, last_reply + 1)
    return ans
function networkBecomesIdle(edges, patience) {
  const n = patience.length;
  const adj = Array.from({ length: n }, () => []);
  for (const [u, v] of edges) {
    adj[u].push(v);
    adj[v].push(u);
  }
  const dist = new Array(n).fill(-1);
  dist[0] = 0;
  const q = [0];
  for (let h = 0; h < q.length; h++) {
    const u = q[h];
    for (const v of adj[u]) {
      if (dist[v] === -1) {
        dist[v] = dist[u] + 1;
        q.push(v);
      }
    }
  }
  let ans = 0;
  for (let i = 1; i < n; i++) {
    const rtt = 2 * dist[i];
    const lastSend = Math.floor((rtt - 1) / patience[i]) * patience[i];
    const lastReply = lastSend + rtt;
    ans = Math.max(ans, lastReply + 1);
  }
  return ans;
}
class Solution {
    public int networkBecomesIdle(int[][] edges, int[] patience) {
        int n = patience.length;
        List<Integer>[] adj = new List[n];
        for (int i = 0; i < n; i++) adj[i] = new ArrayList<>();
        for (int[] e : edges) {
            adj[e[0]].add(e[1]);
            adj[e[1]].add(e[0]);
        }
        int[] dist = new int[n];
        Arrays.fill(dist, -1);
        dist[0] = 0;
        Queue<Integer> q = new LinkedList<>();
        q.offer(0);
        while (!q.isEmpty()) {
            int u = q.poll();
            for (int v : adj[u]) {
                if (dist[v] == -1) {
                    dist[v] = dist[u] + 1;
                    q.offer(v);
                }
            }
        }
        int ans = 0;
        for (int i = 1; i < n; i++) {
            int rtt = 2 * dist[i];
            int lastSend = (rtt - 1) / patience[i] * patience[i];
            int lastReply = lastSend + rtt;
            ans = Math.max(ans, lastReply + 1);
        }
        return ans;
    }
}
class Solution {
public:
    int networkBecomesIdle(vector<vector<int>>& edges, vector<int>& patience) {
        int n = patience.size();
        vector<vector<int>> adj(n);
        for (auto& e : edges) {
            adj[e[0]].push_back(e[1]);
            adj[e[1]].push_back(e[0]);
        }
        vector<int> dist(n, -1);
        dist[0] = 0;
        queue<int> q;
        q.push(0);
        while (!q.empty()) {
            int u = q.front(); q.pop();
            for (int v : adj[u]) {
                if (dist[v] == -1) {
                    dist[v] = dist[u] + 1;
                    q.push(v);
                }
            }
        }
        int ans = 0;
        for (int i = 1; i < n; i++) {
            int rtt = 2 * dist[i];
            int lastSend = (rtt - 1) / patience[i] * patience[i];
            int lastReply = lastSend + rtt;
            ans = max(ans, lastReply + 1);
        }
        return ans;
    }
};
Time: O(V + E) Space: O(V + E)