{"id":6077,"date":"2020-01-11T22:15:27","date_gmt":"2020-01-12T06:15:27","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6077"},"modified":"2020-01-29T19:52:03","modified_gmt":"2020-01-30T03:52:03","slug":"leetcode-1319-number-of-operations-to-make-network-connected","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-1319-number-of-operations-to-make-network-connected\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1319. Number of Operations to Make Network Connected"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1319. Number of Operations to Make Network Connected - \u5237\u9898\u627e\u5de5\u4f5c EP297\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/8d8vfU3LYHM?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>There are&nbsp;<code>n<\/code>&nbsp;computers numbered from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n-1<\/code>&nbsp;connected by&nbsp;ethernet cables&nbsp;<code>connections<\/code>&nbsp;forming a network where&nbsp;<code>connections[i] = [a, b]<\/code>&nbsp;represents a connection between computers&nbsp;<code>a<\/code>&nbsp;and&nbsp;<code>b<\/code>. Any computer&nbsp;can reach any other computer directly or indirectly through the network.<\/p>\n\n\n\n<p>Given an initial computer network&nbsp;<code>connections<\/code>. You can extract certain cables between two directly connected computers, and place them between any pair of disconnected computers to make them directly connected. Return the&nbsp;<em>minimum number of times<\/em>&nbsp;you need to do this in order to make all the computers connected. If it&#8217;s not possible, return -1.&nbsp;<\/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\/01\/02\/sample_1_1677.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4, connections = [[0,1],[0,2],[1,2]]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> Remove cable between computer 1 and 2 and place between computers 1 and 3.\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\/01\/02\/sample_2_1677.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 6, connections = [[0,1],[0,2],[0,3],[1,2],[1,3]]\n<strong>Output:<\/strong> 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 = 6, connections = [[0,1],[0,2],[0,3],[1,2]]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> There are not enough cables.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 5, connections = [[0,1],[0,2],[3,4],[2,3]]\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 10^5<\/code><\/li><li><code>1 &lt;= connections.length &lt;= min(n*(n-1)\/2, 10^5)<\/code><\/li><li><code>connections[i].length == 2<\/code><\/li><li><code>0 &lt;= connections[i][0], connections[i][1]&nbsp;&lt; n<\/code><\/li><li><code>connections[i][0] != connections[i][1]<\/code><\/li><li>There are no repeated connections.<\/li><li>No two computers are connected by more than one cable.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Union-Find<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(V+E)<br>Space complexity: O(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 makeConnected(int n, vector<vector<int>>& connections) {\n    if (connections.size() < n - 1) return -1;\n    vector<int> p(n);\n    iota(begin(p), end(p), 0);\n    \n    function<int(int)> find = [&](int x) {\n      return p[x] == x ? x : p[x] = find(p[x]);\n    };\n    \n    for (const auto& c : connections)\n      p[find(c[0])] = find(c[1]);    \n    \n    unordered_set<int> s;\n    for (int i = 0; i < n; ++i)\n      s.insert(find(i));\n    \n    return s.size() - 1;        \n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: DFS<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(V+E)<br>Space complexity: O(V+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 makeConnected(int n, vector<vector<int>>& connections) {\n    if (connections.size() < n - 1) return -1;\n    vector<vector<int>> g(n);\n    for (const auto& c : connections) {\n      g[c[0]].push_back(c[1]);\n      g[c[1]].push_back(c[0]);\n    }\n    vector<int> seen(n);\n    int count = 0;\n    function<void(int)> dfs = [&](int cur) {\n      for (int nxt : g[cur])\n        if (!seen[nxt]++) dfs(nxt);      \n    };\n    for (int i = 0; i < n; ++i)\n      if (!seen[i]++ &#038;&#038; ++count)\n        dfs(i);        \n    return count - 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are&nbsp;n&nbsp;computers numbered from&nbsp;0&nbsp;to&nbsp;n-1&nbsp;connected by&nbsp;ethernet cables&nbsp;connections&nbsp;forming a network where&nbsp;connections[i] = [a, b]&nbsp;represents a connection between computers&nbsp;a&nbsp;and&nbsp;b. Any computer&nbsp;can reach any other computer directly or indirectly&#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":[33,77,177,499,113],"class_list":["post-6077","post","type-post","status-publish","format-standard","hentry","category-graph","tag-dfs","tag-graph","tag-medium","tag-ove","tag-union-find","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6077","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=6077"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6077\/revisions"}],"predecessor-version":[{"id":6177,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6077\/revisions\/6177"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}