{"id":2196,"date":"2018-03-17T23:37:10","date_gmt":"2018-03-18T06:37:10","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2196"},"modified":"2018-03-19T00:23:35","modified_gmt":"2018-03-19T07:23:35","slug":"leetcode-802-find-eventual-safe-states","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-802-find-eventual-safe-states\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 802. Find Eventual Safe States"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 802. Find Eventual Safe States  - \u5237\u9898\u627e\u5de5\u4f5c EP174\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/v5Ni_3bHjzk?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><\/p>\n<h1>Problem<\/h1>\n<p><a href=\"https:\/\/leetcode.com\/problems\/find-eventual-safe-states\/description\/\">https:\/\/leetcode.com\/problems\/find-eventual-safe-states\/description\/<\/a><\/p>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4e00\u4e2a\u6709\u5411\u56fe\uff0c\u627e\u51fa\u6240\u6709\u4e0d\u53ef\u80fd\u8fdb\u5165\u73af\u7684\u8282\u70b9\u3002<\/p>\n<p>In a directed graph, we start at some node and every turn, walk along a directed edge of the graph.\u00a0 If we reach a node that is terminal (that is, it has no outgoing directed edges), we stop.<\/p>\n<p>Now, say our starting node is\u00a0<em>eventually safe\u00a0<\/em>if and only if we must eventually walk to a terminal node.\u00a0 More specifically, there exists a natural number\u00a0<code>K<\/code>\u00a0so that for any choice of where to walk, we must have stopped at a terminal node in less than\u00a0<code>K<\/code>\u00a0steps.<\/p>\n<p>Which nodes are eventually safe?\u00a0 Return them as an array in sorted order.<\/p>\n<p>The directed graph has\u00a0<code>N<\/code>\u00a0nodes with labels\u00a0<code>0, 1, ..., N-1<\/code>, where\u00a0<code>N<\/code>\u00a0is the length of\u00a0<code>graph<\/code>.\u00a0 The\u00a0graph is given in the following form:\u00a0<code>graph[i]<\/code>\u00a0is a list of labels\u00a0<code>j<\/code>\u00a0such that\u00a0<code>(i, j)<\/code>\u00a0is a directed edge of the graph.<\/p>\n<pre class=\"crayon:false \"><strong>Example:<\/strong>\r\n<strong>Input:<\/strong> graph = [[1,2],[2,3],[5],[0],[5],[],[]]\r\n<strong>Output:<\/strong> [2,4,5,6]\r\nHere is a diagram of the above graph.\r\n\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/s3-lc-upload.s3.amazonaws.com\/uploads\/2018\/03\/17\/picture1.png\" alt=\"Illustration of graph\" \/><\/p>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li><code>graph<\/code>\u00a0will have length at most\u00a0<code>10000<\/code>.<\/li>\n<li>The number of edges in the graph will not exceed\u00a0<code>32000<\/code>.<\/li>\n<li>Each\u00a0<code>graph[i]<\/code>\u00a0will be a sorted list of different integers, chosen within the range\u00a0<code>[0, graph.length - 1]<\/code>.<\/li>\n<\/ul>\n<h1><strong>Idea: Finding Cycles<\/strong><\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2214\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/802-ep174.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/802-ep174.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/802-ep174-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/802-ep174-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<h1><strong>Solution 1: DFS<\/strong><\/h1>\n<p><strong>\u00a0<\/strong>A node is safe if and only if: itself and all of its neighbors do not have any cycles.<\/p>\n<p>Time complexity: O(V + E)<\/p>\n<p>Space complexity: O(V + E)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 155 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; eventualSafeNodes(vector&lt;vector&lt;int&gt;&gt;&amp; graph) {    \r\n    vector&lt;State&gt; states(graph.size(), UNKNOWN);\r\n    vector&lt;int&gt; ans;\r\n    for (int i = 0; i &lt; graph.size(); ++i)      \r\n      if (dfs(graph, i, states) == SAFE)\r\n        ans.push_back(i);\r\n    return ans;\r\n  }\r\nprivate:\r\n  enum State {UNKNOWN, VISITING, SAFE, UNSAFE};\r\n  State dfs(const vector&lt;vector&lt;int&gt;&gt;&amp; g, int cur, vector&lt;State&gt;&amp; states) {\r\n    if (states[cur] == VISITING)\r\n      return states[cur] = UNSAFE;\r\n    if (states[cur] != UNKNOWN)\r\n      return states[cur];\r\n    states[cur] = VISITING;\r\n    for (int next : g[cur])\r\n      if (dfs(g, next, states) == UNSAFE) \r\n        return states[cur] = UNSAFE;\r\n    return states[cur] = SAFE;\r\n  }\r\n};<\/pre>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-207-course-schedule\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 207. Course Schedule<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-210-course-schedule-ii\/\">\u82b1\u82b1\u9171 LeetCode 210. Course Schedule II<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-684-redundant-connection\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 684. Redundant Connection \u82b1\u82b1\u9171<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem https:\/\/leetcode.com\/problems\/find-eventual-safe-states\/description\/ \u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4e00\u4e2a\u6709\u5411\u56fe\uff0c\u627e\u51fa\u6240\u6709\u4e0d\u53ef\u80fd\u8fdb\u5165\u73af\u7684\u8282\u70b9\u3002 In a directed graph, we start at some node and every turn, walk along a directed edge of the graph.\u00a0 If we&#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":[123,33,124,77,177,137],"class_list":["post-2196","post","type-post","status-publish","format-standard","hentry","category-graph","tag-cycle","tag-dfs","tag-directed-graph","tag-graph","tag-medium","tag-topological-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2196","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=2196"}],"version-history":[{"count":15,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2196\/revisions"}],"predecessor-version":[{"id":2227,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2196\/revisions\/2227"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}