{"id":5556,"date":"2019-09-15T16:07:27","date_gmt":"2019-09-15T23:07:27","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5556"},"modified":"2019-09-16T21:37:32","modified_gmt":"2019-09-17T04:37:32","slug":"leetcode-1192-critical-connections-in-a-network","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-1192-critical-connections-in-a-network\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1192. Critical Connections in a Network"},"content":{"rendered":"\n<p>There are&nbsp;<code>n<\/code>&nbsp;servers numbered from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n-1<\/code>&nbsp;connected by&nbsp;undirected server-to-server&nbsp;<code>connections<\/code>&nbsp;forming a network where&nbsp;<code>connections[i] = [a, b]<\/code>&nbsp;represents a connection between servers&nbsp;<code>a<\/code>&nbsp;and&nbsp;<code>b<\/code>. Any server can reach any other server directly or indirectly through the network.<\/p>\n\n\n\n<p>A&nbsp;<em>critical connection<\/em>&nbsp;is a connection that, if removed, will make some server unable to reach some other server.<\/p>\n\n\n\n<p>Return all critical connections in the network in any order.<\/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\/2019\/09\/03\/1537_ex1_2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4, connections = [[0,1],[1,2],[2,0],[1,3]]\n<strong>Output:<\/strong> [[1,3]]\n<strong>Explanation:<\/strong> [[3,1]] is also accepted.\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>n-1 &lt;= connections.length &lt;= 10^5<\/code><\/li><li><code>connections[i][0] != connections[i][1]<\/code><\/li><li>There are no repeated connections.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Tarjan<\/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  vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections) {\n    vector<vector<int>> g(n);\n    vector<int> ts(n, INT_MAX);\n    vector<vector<int>> ans;\n    int t = 0;\n    \n    function<int(int, int)> tarjan = [&](int i, int p) {\n      int min_i = ts[i] = ++t;\n      for (int j : g[i]) {\n        if (ts[j] == INT_MAX) {\n          int min_j = tarjan(j, i);\n          min_i = min(min_i, min_j);\n          if (ts[i] < min_j)\n            ans.push_back({i, j});\n        } else if (j != p) {\n          min_i = min(min_i, ts[j]);\n        }\n      }\n      return min_i;\n    };\n    \n    for (const auto&#038; e : connections) {\n      g[e[0]].push_back(e[1]);\n      g[e[1]].push_back(e[0]);\n    }\n    \n    tarjan(0, -1);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are&nbsp;n&nbsp;servers numbered from&nbsp;0&nbsp;to&nbsp;n-1&nbsp;connected by&nbsp;undirected server-to-server&nbsp;connections&nbsp;forming a network where&nbsp;connections[i] = [a, b]&nbsp;represents a connection between servers&nbsp;a&nbsp;and&nbsp;b. Any server can reach any other server directly or&#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":[77,217,499,500],"class_list":["post-5556","post","type-post","status-publish","format-standard","hentry","category-graph","tag-graph","tag-hard","tag-ove","tag-tarjan","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5556","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=5556"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5556\/revisions"}],"predecessor-version":[{"id":5564,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5556\/revisions\/5564"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}