{"id":7477,"date":"2020-10-10T22:14:11","date_gmt":"2020-10-11T05:14:11","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7477"},"modified":"2020-10-10T22:14:36","modified_gmt":"2020-10-11T05:14:36","slug":"leetcode-1615-maximal-network-rank","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-1615-maximal-network-rank\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1615. Maximal Network Rank"},"content":{"rendered":"\n<p>There is an infrastructure of&nbsp;<code>n<\/code>&nbsp;cities with some number of&nbsp;<code>roads<\/code>&nbsp;connecting these cities. Each&nbsp;<code>roads[i] = [a<sub>i<\/sub>, b<sub>i<\/sub>]<\/code>&nbsp;indicates that there is a bidirectional road between cities&nbsp;<code>a<sub>i<\/sub><\/code>&nbsp;and&nbsp;<code>b<sub>i<\/sub><\/code>.<\/p>\n\n\n\n<p>The&nbsp;<strong>network rank<\/strong>of&nbsp;<strong>two different cities<\/strong>&nbsp;is defined as the total number of&nbsp;<strong>directly<\/strong>&nbsp;connected roads to&nbsp;<strong>either<\/strong>&nbsp;city. If a road is directly connected to both cities, it is only counted&nbsp;<strong>once<\/strong>.<\/p>\n\n\n\n<p>The&nbsp;<strong>maximal network rank&nbsp;<\/strong>of the infrastructure is the&nbsp;<strong>maximum network rank<\/strong>&nbsp;of all pairs of different cities.<\/p>\n\n\n\n<p>Given the integer&nbsp;<code>n<\/code>&nbsp;and the array&nbsp;<code>roads<\/code>, return&nbsp;<em>the&nbsp;<strong>maximal network rank<\/strong>&nbsp;of the entire infrastructure<\/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\/2020\/09\/21\/ex1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4, roads = [[0,1],[0,3],[1,2],[1,3]]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> The network rank of cities 0 and 1 is 4 as there are 4 roads that are connected to either 0 or 1. The road between 0 and 1 is only counted once.\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\/2020\/09\/21\/ex2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 5, roads = [[0,1],[0,3],[1,2],[1,3],[2,3],[2,4]]\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> There are 5 roads that are connected to cities 1 or 2.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 8, roads = [[0,1],[1,2],[2,3],[2,4],[5,6],[5,7]]\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> The network rank of 2 and 5 is 5. Notice that all the cities do not have to be connected.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= n &lt;= 100<\/code><\/li><li><code>0 &lt;= roads.length &lt;= n * (n - 1) \/ 2<\/code><\/li><li><code>roads[i].length == 2<\/code><\/li><li><code>0 &lt;= a<sub>i<\/sub>, b<sub>i<\/sub>&nbsp;&lt;= n-1<\/code><\/li><li><code>a<sub>i<\/sub>&nbsp;!=&nbsp;b<sub>i<\/sub><\/code><\/li><li>Each&nbsp;pair of cities has&nbsp;<strong>at most one<\/strong>&nbsp;road connecting them.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Counting degrees and all pairs<\/strong><\/h2>\n\n\n\n<p>Counting degrees for each node, if a and b are not connected, ans = degrees(a) + degrees(b), otherwise ans -= 1<\/p>\n\n\n\n<p>Time complexity: O(E + V^2)<br>Space complexity: O(E)<\/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 maximalNetworkRank(int n, vector<vector<int>>& roads) {\n    vector<int> degrees(n);\n    unordered_set<int> connected;\n    for (const auto& road : roads) {\n      ++degrees[road[0]];\n      ++degrees[road[1]];\n      connected.insert((road[0] << 16) | road[1]);\n      connected.insert((road[1] << 16) | road[0]);\n    }\n    int ans = 0;\n    for (int i = 0; i < n; ++i)\n      for (int j = i + 1; j < n; ++j)\n        ans = max(ans, degrees[i] + degrees[j] \n                         - (connected.count((i << 16) | j) ? 1 : 0));\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is an infrastructure of&nbsp;n&nbsp;cities with some number of&nbsp;roads&nbsp;connecting these cities. Each&nbsp;roads[i] = [ai, bi]&nbsp;indicates that there is a bidirectional road between cities&nbsp;ai&nbsp;and&nbsp;bi. The&nbsp;network rankof&nbsp;two&#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":[413,659,77,177],"class_list":["post-7477","post","type-post","status-publish","format-standard","hentry","category-graph","tag-all-pairs","tag-degrees","tag-graph","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7477","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=7477"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7477\/revisions"}],"predecessor-version":[{"id":7479,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7477\/revisions\/7479"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}