{"id":8623,"date":"2021-10-18T21:53:38","date_gmt":"2021-10-19T04:53:38","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8623"},"modified":"2021-10-18T22:01:47","modified_gmt":"2021-10-19T05:01:47","slug":"leetcode-2039-the-time-when-the-network-becomes-idle","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-2039-the-time-when-the-network-becomes-idle\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2039. The Time When the Network Becomes Idle"},"content":{"rendered":"\n<p>There is a network of&nbsp;<code>n<\/code>&nbsp;servers, labeled from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>. You are given a 2D integer array&nbsp;<code>edges<\/code>, where&nbsp;<code>edges[i] = [u<sub>i<\/sub>, v<sub>i<\/sub>]<\/code>&nbsp;indicates there is a message channel between servers&nbsp;<code>u<sub>i<\/sub><\/code>&nbsp;and&nbsp;<code>v<sub>i<\/sub><\/code>, and they can pass&nbsp;<strong>any<\/strong>&nbsp;number of messages to&nbsp;<strong>each other<\/strong>&nbsp;directly in&nbsp;<strong>one<\/strong>&nbsp;second. You are also given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>patience<\/code>&nbsp;of length&nbsp;<code>n<\/code>.<\/p>\n\n\n\n<p>All servers are&nbsp;<strong>connected<\/strong>, i.e., a message can be passed from one server to any other server(s) directly or indirectly through the message channels.<\/p>\n\n\n\n<p>The server labeled&nbsp;<code>0<\/code>&nbsp;is the&nbsp;<strong>master<\/strong>&nbsp;server. The rest are&nbsp;<strong>data<\/strong>&nbsp;servers. Each data server needs to send its message to the master server for processing and wait for a reply. Messages move between servers&nbsp;<strong>optimally<\/strong>, so every message takes the&nbsp;<strong>least amount of time<\/strong>&nbsp;to arrive at the master server. The master server will process all newly arrived messages&nbsp;<strong>instantly<\/strong>&nbsp;and send a reply to the originating server via the&nbsp;<strong>reversed path<\/strong>&nbsp;the message had gone through.<\/p>\n\n\n\n<p>At the beginning of second&nbsp;<code>0<\/code>, each data server sends its message to be processed. Starting from second&nbsp;<code>1<\/code>, at the&nbsp;<strong>beginning<\/strong>&nbsp;of&nbsp;<strong>every<\/strong>&nbsp;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:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If it has not, it will&nbsp;<strong>resend<\/strong>&nbsp;the message periodically. The data server&nbsp;<code>i<\/code>&nbsp;will resend the message every&nbsp;<code>patience[i]<\/code>&nbsp;second(s), i.e., the data server&nbsp;<code>i<\/code>&nbsp;will resend the message if&nbsp;<code>patience[i]<\/code>&nbsp;second(s) have&nbsp;<strong>elapsed<\/strong>&nbsp;since the&nbsp;<strong>last<\/strong>&nbsp;time the message was sent from this server.<\/li><li>Otherwise,&nbsp;<strong>no more resending<\/strong>&nbsp;will occur from this server.<\/li><\/ul>\n\n\n\n<p>The network becomes&nbsp;<strong>idle<\/strong>&nbsp;when there are&nbsp;<strong>no<\/strong>&nbsp;messages passing between servers or arriving at servers.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>earliest second<\/strong>&nbsp;starting from which the network becomes&nbsp;<strong>idle<\/strong><\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/09\/22\/quiet-place-example1.png\" alt=\"example 1\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> edges = [[0,1],[1,2]], patience = [0,2,1]\n<strong>Output:<\/strong> 8\n<strong>Explanation:<\/strong>\nAt (the beginning of) second 0,\n- Data server 1 sends its message (denoted 1A) to the master server.\n- Data server 2 sends its message (denoted 2A) to the master server.\n\nAt second 1,\n- Message 1A arrives at the master server. Master server processes message 1A instantly and sends a reply 1A back.\n- Server 1 has not received any reply. 1 second (1 &lt; patience[1] = 2) elapsed since this server has sent the message, therefore it does not resend the message.\n- Server 2 has not received any reply. 1 second (1 == patience[2] = 1) elapsed since this server has sent the message, therefore it resends the message (denoted 2B).\n\nAt second 2,\n- The reply 1A arrives at server 1. No more resending will occur from server 1.\n- Message 2A arrives at the master server. Master server processes message 2A instantly and sends a reply 2A back.\n- Server 2 resends the message (denoted 2C).\n...\nAt second 4,\n- The reply 2A arrives at server 2. No more resending will occur from server 2.\n...\nAt second 7, reply 2D arrives at server 2.\n\nStarting from the beginning of the second 8, there are no messages passing between servers or arriving at servers.\nThis is the time when the network becomes idle.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/09\/04\/network_a_quiet_place_2.png\" alt=\"example 2\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> edges = [[0,1],[0,2],[1,2]], patience = [0,10,10]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> Data servers 1 and 2 receive a reply back at the beginning of second 2.\nFrom the beginning of the second 3, the network becomes idle.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == patience.length<\/code><\/li><li><code>2 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>patience[0] == 0<\/code><\/li><li><code>1 &lt;= patience[i] &lt;= 10<sup>5<\/sup><\/code>&nbsp;for&nbsp;<code>1 &lt;= i &lt; n<\/code><\/li><li><code>1 &lt;= edges.length &lt;= min(10<sup>5<\/sup>, n * (n - 1) \/ 2)<\/code><\/li><li><code>edges[i].length == 2<\/code><\/li><li><code>0 &lt;= u<sub>i<\/sub>, v<sub>i<\/sub>&nbsp;&lt; n<\/code><\/li><li><code>u<sub>i<\/sub>&nbsp;!= v<sub>i<\/sub><\/code><\/li><li>There are no duplicate edges.<\/li><li>Each server can directly or indirectly reach another server.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Shortest Path<\/strong><\/h2>\n\n\n\n<p>Compute the shortest path from node 0 to rest of the nodes using BFS.<\/p>\n\n\n\n<p>Idle time for node i = (dist[i] * 2 &#8211; 1) \/ patince[i] * patience[i] + dist[i] * 2 + 1<\/p>\n\n\n\n<p>Time complexity: O(E + V)<br>Space complexity: O(E + V)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int networkBecomesIdle(vector<vector<int>>& edges, vector<int>& patience) {\n    const int n = patience.size();\n    vector<vector<int>> g(n);\n    for (const auto& e : edges) {\n      g[e[0]].push_back(e[1]);\n      g[e[1]].push_back(e[0]);\n    }\n    vector<int> ts(n, -1);\n    ts[0] = 0;\n    queue<int> q{{0}};    \n    while (!q.empty()) {      \n      int u = q.front(); q.pop();        \n      for (int v : g[u]) {\n        if (ts[v] != -1) continue;\n        ts[v] = ts[u] + 1;\n        q.push(v);\n      }\n    }\n    int ans = 0;\n    for (int i = 1; i < n; ++i) {      \n      int t = (ts[i] * 2 - 1) \/ patience[i] * patience[i] + ts[i] * 2 + 1;\n      ans = max(ans, t);\n    }    \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is a network of&nbsp;n&nbsp;servers, labeled from&nbsp;0&nbsp;to&nbsp;n &#8211; 1. You are given a 2D integer array&nbsp;edges, where&nbsp;edges[i] = [ui, vi]&nbsp;indicates there is a message channel&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[34,77,177,87],"class_list":["post-8623","post","type-post","status-publish","format-standard","hentry","category-graph","tag-bfs","tag-graph","tag-medium","tag-shortest-path","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8623"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8623\/revisions"}],"predecessor-version":[{"id":8626,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8623\/revisions\/8626"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}