{"id":9761,"date":"2022-06-25T16:07:42","date_gmt":"2022-06-25T23:07:42","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9761"},"modified":"2022-06-25T16:11:20","modified_gmt":"2022-06-25T23:11:20","slug":"leetcode-2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2316. Count Unreachable Pairs of Nodes in an Undirected Graph"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-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 2316. Count Unreachable Pairs of Nodes in an Undirected Graph - \u5237\u9898\u627e\u5de5\u4f5c EP397\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/vH9CLfE82Rk?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>You are given an integer\u00a0<code>n<\/code>. There is an\u00a0<strong>undirected<\/strong>\u00a0graph with\u00a0<code>n<\/code>\u00a0nodes, numbered from\u00a0<code>0<\/code>\u00a0to\u00a0<code>n - 1<\/code>. You are given a 2D integer array\u00a0<code>edges<\/code>\u00a0where\u00a0<code>edges[i] = [a<sub>i<\/sub>, b<sub>i<\/sub>]<\/code>\u00a0denotes that there exists an\u00a0<strong>undirected<\/strong>\u00a0edge connecting nodes\u00a0<code>a<sub>i<\/sub><\/code>\u00a0and\u00a0<code>b<sub>i<\/sub><\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>number of pairs<\/strong>&nbsp;of different nodes that are&nbsp;<strong>unreachable<\/strong>&nbsp;from each other<\/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\/2022\/05\/05\/tc-3.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 3, edges = [[0,1],[0,2],[1,2]]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There are no pairs of nodes that are unreachable from each other. Therefore, we return 0.\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\/2022\/05\/05\/tc-2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 7, edges = [[0,2],[0,5],[2,4],[1,6],[5,4]]\n<strong>Output:<\/strong> 14\n<strong>Explanation:<\/strong> There are 14 pairs of nodes that are unreachable from each other:\n[[0,1],[0,3],[0,6],[1,2],[1,3],[1,4],[1,5],[2,3],[2,6],[3,4],[3,5],[3,6],[4,6],[5,6]].\nTherefore, we return 14.\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<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= edges.length &lt;= 2 * 10<sup>5<\/sup><\/code><\/li><li><code>edges[i].length == 2<\/code><\/li><li><code>0 &lt;= a<sub>i<\/sub>, b<sub>i<\/sub>&nbsp;&lt; n<\/code><\/li><li><code>a<sub>i<\/sub>&nbsp;!= b<sub>i<\/sub><\/code><\/li><li>There are no repeated edges.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: DFS<\/strong><\/h2>\n\n\n\n<p>Use DFS to find all CCs<\/p>\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\n\/\/ Author: Huahua, 791ms, 136 MB\nclass Solution {\npublic:\n  long long countPairs(int n, vector<vector<int>>& edges) {\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> seen(n);\n    long long cur = 0;\n    \n    function<void(int)> dfs = [&](int u) {\n      ++cur;\n      for (int v : g[u])\n        if (seen[v]++ == 0) dfs(v);      \n    };\n    long long ans = 0;    \n    for (int i = 0; i < n; ++i) {\n      if (seen[i]++) continue;\n      cur = 0;\n      dfs(i);\n      ans += (n - cur) * cur;\n    }\n    return ans \/ 2;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: 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  long long countPairs(int n, vector<vector<int>>& edges) {\n    vector<int> parents(n);\n    vector<int> counts(n, 1);\n    std::iota(begin(parents), end(parents), 0);\n    \n    function<int(int)> find = [&](int x) {\n      if (parents[x] == x) return x;\n      return parents[x] = find(parents[x]);\n    };\n    \n    for (const auto& e : edges) {\n      int ru = find(e[0]);\n      int rv = find(e[1]);\n      if (ru != rv) {\n        parents[rv] = ru;\n        counts[ru] += counts[rv];        \n      }\n    }\n    long long ans = 0;    \n    for (int i = 0; i < n; ++i)      \n      ans += n - counts[find(i)];\n    return ans \/ 2;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer\u00a0n. There is an\u00a0undirected\u00a0graph with\u00a0n\u00a0nodes, numbered from\u00a00\u00a0to\u00a0n &#8211; 1. You are given a 2D integer array\u00a0edges\u00a0where\u00a0edges[i] = [ai, bi]\u00a0denotes that there&#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,113],"class_list":["post-9761","post","type-post","status-publish","format-standard","hentry","category-graph","tag-dfs","tag-graph","tag-medium","tag-union-find","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9761","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=9761"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9761\/revisions"}],"predecessor-version":[{"id":9764,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9761\/revisions\/9764"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}