{"id":6841,"date":"2020-05-30T18:07:46","date_gmt":"2020-05-31T01:07:46","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6841"},"modified":"2020-05-30T18:09:59","modified_gmt":"2020-05-31T01:09:59","slug":"leetcode-1462-course-schedule-iv","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-1462-course-schedule-iv\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1462. Course Schedule IV"},"content":{"rendered":"\n<p>There are a total of&nbsp;<code>n<\/code>&nbsp;courses you have to take, labeled from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n-1<\/code>.<\/p>\n\n\n\n<p>Some courses may have direct prerequisites, for example, to take course 0 you have first to take course 1, which is expressed as a pair:&nbsp;<code>[1,0]<\/code><\/p>\n\n\n\n<p>Given the total number of courses&nbsp;<code>n<\/code>,&nbsp;a list of direct&nbsp;<code>prerequisite<\/code>&nbsp;<strong>pairs<\/strong>&nbsp;and a list of&nbsp;<code>queries<\/code>&nbsp;<strong>pairs<\/strong>.<\/p>\n\n\n\n<p>You should answer for each&nbsp;<code>queries[i]<\/code>&nbsp;whether the course&nbsp;<code>queries[i][0]<\/code>&nbsp;is a&nbsp;prerequisite of the course&nbsp;<code>queries[i][1]<\/code>&nbsp;or not.<\/p>\n\n\n\n<p>Return&nbsp;<em>a list of boolean<\/em>, the answers to the given&nbsp;<code>queries<\/code>.<\/p>\n\n\n\n<p>Please note that if course&nbsp;<strong>a<\/strong>&nbsp;is a prerequisite of course&nbsp;<strong>b<\/strong>&nbsp;and course&nbsp;<strong>b<\/strong>&nbsp;is a prerequisite&nbsp;of course&nbsp;<strong>c<\/strong>, then, course&nbsp;<strong>a<\/strong>&nbsp;is a&nbsp;prerequisite of course&nbsp;<strong>c<\/strong>.<\/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\/04\/17\/graph.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 2, prerequisites = [[1,0]], queries = [[0,1],[1,0]]\n<strong>Output:<\/strong> [false,true]\n<strong>Explanation:<\/strong> course 0 is not a prerequisite of course 1 but the opposite is true.\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 = 2, prerequisites = [], queries = [[1,0],[0,1]]\n<strong>Output:<\/strong> [false,false]\n<strong>Explanation:<\/strong> There are no prerequisites and each course is independent.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/04\/17\/graph-1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 3, prerequisites = [[1,2],[1,0],[2,0]], queries = [[1,0],[1,2]]\n<strong>Output:<\/strong> [true,true]\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, prerequisites = [[1,0],[2,0]], queries = [[0,1],[2,0]]\n<strong>Output:<\/strong> [false,true]\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 = 5, prerequisites = [[0,1],[1,2],[2,3],[3,4]], queries = [[0,4],[4,0],[1,3],[3,0]]\n<strong>Output:<\/strong> [true,false,true,false]\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;= 100<\/code><\/li><li><code>0 &lt;= prerequisite.length &lt;= (n * (n - 1) \/ 2)<\/code><\/li><li><code>0 &lt;= prerequisite[i][0], prerequisite[i][1] &lt; n<\/code><\/li><li><code>prerequisite[i][0] != prerequisite[i][1]<\/code><\/li><li>The prerequisites graph has no cycles.<\/li><li>The prerequisites graph has no repeated edges.<\/li><li><code>1 &lt;= queries.length &lt;= 10^4<\/code><\/li><li><code>queries[i][0] != queries[i][1]<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Floyd-Warshall Algorithm (All pairs shortest paths<\/strong>)<\/h2>\n\n\n\n<p>Time complexity: O(n^3 + q)<br>Space complexity: O(n^2)<\/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<bool> checkIfPrerequisite(int n, \n      vector<vector<int>>& prerequisites, vector<vector<int>>& queries) {\n    vector<vector<int>> g(n, vector<int>(n));\n    for (const auto& p : prerequisites)\n      g[p[0]][p[1]] = 1;\n    for (int k = 0; k < n; ++k)\n      for (int i = 0; i < n; ++i)\n        for (int j = 0; j < n; ++j)          \n          g[i][j] |= g[i][k] &#038; g[k][j];\n    vector<bool> ans;\n    for (const auto& q : queries)\n      ans.push_back(g[q[0]][q[1]]);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are a total of&nbsp;n&nbsp;courses you have to take, labeled from&nbsp;0&nbsp;to&nbsp;n-1. Some courses may have direct prerequisites, for example, to take course 0 you have&#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":[612,77],"class_list":["post-6841","post","type-post","status-publish","format-standard","hentry","category-graph","tag-all-pair-shortest-paths","tag-graph","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6841","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=6841"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6841\/revisions"}],"predecessor-version":[{"id":6843,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6841\/revisions\/6843"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}