{"id":7275,"date":"2020-08-22T11:31:08","date_gmt":"2020-08-22T18:31:08","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7275"},"modified":"2020-08-22T11:31:38","modified_gmt":"2020-08-22T18:31:38","slug":"leetcode-1557-minimum-number-of-vertices-to-reach-all-nodes","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-1557-minimum-number-of-vertices-to-reach-all-nodes\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1557. Minimum Number of Vertices to Reach All Nodes"},"content":{"rendered":"\n<p>Given a<strong>&nbsp;directed acyclic graph<\/strong>,&nbsp;with&nbsp;<code>n<\/code>&nbsp;vertices numbered from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n-1<\/code>,&nbsp;and an array&nbsp;<code>edges<\/code>&nbsp;where&nbsp;<code>edges[i] = [from<sub>i<\/sub>, to<sub>i<\/sub>]<\/code>&nbsp;represents a directed edge from node&nbsp;<code>from<sub>i<\/sub><\/code>&nbsp;to node&nbsp;<code>to<sub>i<\/sub><\/code>.<\/p>\n\n\n\n<p>Find&nbsp;<em>the smallest set of vertices from which all nodes in the graph are reachable<\/em>. It&#8217;s guaranteed that a unique solution exists.<\/p>\n\n\n\n<p>Notice that you can return the vertices 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\/2020\/07\/07\/untitled22.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 6, edges = [[0,1],[0,2],[2,5],[3,4],[4,2]]\n<strong>Output:<\/strong> [0,3]\n<strong>Explanation: <\/strong>It's not possible to reach all the nodes from a single vertex. From 0 we can reach [0,1,2,5]. From 3 we can reach [3,4,2,5]. So we output [0,3].<\/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\/07\/07\/untitled.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 5, edges = [[0,1],[2,1],[3,1],[1,4],[2,4]]\n<strong>Output:<\/strong> [0,2,3]\n<strong>Explanation: <\/strong>Notice that vertices 0, 3 and 2 are not reachable from any other node, so we must include them. Also any of these vertices can reach nodes 1 and 4.\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;= 10^5<\/code><\/li><li><code>1 &lt;= edges.length &lt;= min(10^5, n * (n - 1) \/ 2)<\/code><\/li><li><code>edges[i].length == 2<\/code><\/li><li><code>0 &lt;= from<sub>i,<\/sub>&nbsp;to<sub>i<\/sub>&nbsp;&lt; n<\/code><\/li><li>All pairs&nbsp;<code>(from<sub>i<\/sub>, to<sub>i<\/sub>)<\/code>&nbsp;are distinct.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: In degree<\/strong><\/h2>\n\n\n\n<p>Nodes with 0 in degree will be the answer.<br>Time complexity: O(E+V)<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++\">\nclass Solution {\npublic:\n  vector<int> findSmallestSetOfVertices(int n, vector<vector<int>>& edges) {\n    vector<int> in(n);    \n    for (const auto& e : edges) ++in[e[1]];\n    vector<int> ans;\n    for (int i = 0; i < n; ++i)\n      if (in[i] == 0) ans.push_back(i);\n    return ans;    \n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;directed acyclic graph,&nbsp;with&nbsp;n&nbsp;vertices numbered from&nbsp;0&nbsp;to&nbsp;n-1,&nbsp;and an array&nbsp;edges&nbsp;where&nbsp;edges[i] = [fromi, toi]&nbsp;represents a directed edge from node&nbsp;fromi&nbsp;to node&nbsp;toi. Find&nbsp;the smallest set of vertices from which all&#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,560,177],"class_list":["post-7275","post","type-post","status-publish","format-standard","hentry","category-graph","tag-graph","tag-in-degree","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7275","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=7275"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7275\/revisions"}],"predecessor-version":[{"id":7277,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7275\/revisions\/7277"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}