{"id":5317,"date":"2019-07-20T22:03:19","date_gmt":"2019-07-21T05:03:19","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5317"},"modified":"2019-07-22T08:21:27","modified_gmt":"2019-07-22T15:21:27","slug":"leetcode-1129-shortest-path-with-alternating-colors","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-1129-shortest-path-with-alternating-colors\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1129. Shortest Path with Alternating Colors"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1129. Shortest Path with Alternating Colors - \u5237\u9898\u627e\u5de5\u4f5c EP258\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/ZobIimNrSFA?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>Consider a directed graph, with nodes labelled&nbsp;<code>0, 1, ..., n-1<\/code>.&nbsp; In this graph, each edge is either red or blue, and there could&nbsp;be self-edges or parallel edges.<\/p>\n\n\n\n<p>Each&nbsp;<code>[i, j]<\/code>&nbsp;in&nbsp;<code>red_edges<\/code>&nbsp;denotes a red directed edge from node&nbsp;<code>i<\/code>&nbsp;to node&nbsp;<code>j<\/code>.&nbsp; Similarly, each&nbsp;<code>[i, j]<\/code>&nbsp;in&nbsp;<code>blue_edges<\/code>&nbsp;denotes a blue directed edge from node&nbsp;<code>i<\/code>&nbsp;to node&nbsp;<code>j<\/code>.<\/p>\n\n\n\n<p>Return an array&nbsp;<code>answer<\/code>&nbsp;of length&nbsp;<code>n<\/code>,&nbsp;where each&nbsp;<code>answer[X]<\/code>&nbsp;is&nbsp;the length of the shortest path from node&nbsp;<code>0<\/code>&nbsp;to node&nbsp;<code>X<\/code>&nbsp;such that the edge colors alternate along the path (or&nbsp;<code>-1<\/code>&nbsp;if such a path doesn&#8217;t exist).<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input:<\/strong> n = 3, red_edges = [[0,1],[1,2]], blue_edges = []\n<strong>Output:<\/strong> [0,1,-1]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input:<\/strong> n = 3, red_edges = [[0,1]], blue_edges = [[2,1]]\n<strong>Output:<\/strong> [0,1,-1]\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 = 3, red_edges = [[1,0]], blue_edges = [[2,1]]\n<strong>Output:<\/strong> [0,-1,-1]\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 = 3, red_edges = [[0,1]], blue_edges = [[1,2]]\n<strong>Output:<\/strong> [0,1,2]\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input:<\/strong> n = 3, red_edges = [[0,1],[0,2]], blue_edges = [[1,0]]\n<strong>Output:<\/strong> [0,1,1]\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;= 100<\/code><\/li><li><code>red_edges.length &lt;= 400<\/code><\/li><li><code>blue_edges.length &lt;= 400<\/code><\/li><li><code>red_edges[i].length == blue_edges[i].length == 2<\/code><\/li><li><code>0 &lt;= red_edges[i][j], blue_edges[i][j] &lt; n<\/code><\/li><\/ul>\n\n\n\n<p><strong>Solution: BFS<\/strong><\/p>\n\n\n\n<p>Time complexity: O(|V| + |E|)<br>Space complexity:  O(|V| + |E|)  <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/07\/1129-ep258-1.png\" alt=\"\" class=\"wp-image-5329\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/07\/1129-ep258-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/07\/1129-ep258-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/07\/1129-ep258-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\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> shortestAlternatingPaths(int n, vector<vector<int>>& red_edges, vector<vector<int>>& blue_edges) {\n    vector<unordered_set<int>> edges_r(n);\n    vector<unordered_set<int>> edges_b(n);\n    for (const auto& e : red_edges)\n      edges_r[e[0]].insert(e[1]);\n    for (const auto& e : blue_edges)\n      edges_b[e[0]].insert(e[1]);\n    unordered_set<int> seen_r;\n    unordered_set<int> seen_b;\n    vector<int> ans(n, -1);\n    queue<pair<int, int>> q; \/\/ (node, color)\n    q.push({0, 0}); \/\/ red\n    q.push({0, 1}); \/\/ blue\n    seen_r.insert(0);\n    seen_b.insert(0);\n    int steps = 0;\n    while (!q.empty()) {\n      int size = q.size();\n      while (size--) {\n        int p = q.front().first;\n        int is_red = q.front().second;        \n        q.pop();        \n        ans[p] = ans[p] >= 0 ? min(ans[p], steps) : steps;     \n        const auto& edges = is_red ? edges_r : edges_b;\n        auto& seen = is_red ? seen_r : seen_b;\n        for (int nxt : edges[p]) {\n          if (seen.count(nxt)) continue;\n          seen.insert(nxt);\n          q.push({nxt, 1 - is_red});\n        }\n      }\n      ++steps;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Consider a directed graph, with nodes labelled&nbsp;0, 1, &#8230;, n-1.&nbsp; In this graph, each edge is either red or blue, and there could&nbsp;be self-edges 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":[34,77,42],"class_list":["post-5317","post","type-post","status-publish","format-standard","hentry","category-graph","tag-bfs","tag-graph","tag-search","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5317","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=5317"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5317\/revisions"}],"predecessor-version":[{"id":5330,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5317\/revisions\/5330"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}