# 课程表

你这个学期必须选修 numCourses 门课程,记为 0 到 numCourses - 1

在选修某些课程之前需要一些先修课程。 先修课程按数组 prerequisites 给出,其中 prerequisites[i] = [ai, bi] ,表示如果要学习课程 ai必须 先学习课程  bi

请你判断是否可能完成所有课程的学习?如果可以,返回 true ;否则,返回 false

 

示例 1:

输入:numCourses = 2, prerequisites = [[1,0]]
输出:true
解释:总共有 2 门课程。学习课程 1 之前,你需要完成课程 0 。这是可能的。

示例 2:

输入:numCourses = 2, prerequisites = [[1,0],[0,1]]
输出:false
解释:总共有 2 门课程。学习课程 1 之前,你需要先完成​课程 0 ;并且学习课程 0 之前,你还应先完成课程 1 。这是不可能的。

 

提示:

以下错误的选项是?

## aop ### before ```cpp #include using namespace std; ``` ### after ```cpp ``` ## 答案 ```cpp class Solution { public: bool canFinish(int numCourses, vector> &prerequisites) { unordered_map> rels; int *inDs = new int[numCourses]; for (int i = 0; i < numCourses; i++) { inDs[i] = 0; vector temp; rels[i] = temp; } for (int i = 0; i < prerequisites.size(); i++) { rels[prerequisites[i][1]].push_back(prerequisites[i][0]); inDs[prerequisites[i][0]]++; } queue zeroInD; int num = 0; while (num != numCourses) { for (int i = 0; i < numCourses; i++) { if (inDs[i] == 0) { zeroInD.push(i); num++; inDs[i] = -1; } } if (zeroInD.empty() && num != numCourses) return false; while (!zeroInD.empty()) { int topV = zeroInD.front(); zeroInD.pop(); vector topVEdges = rels[topV]; for (int j = 0; j < topVEdges.size(); j++) { inDs[topVEdges[j]]++; } } } if (num == numCourses) return true; else return false; } }; ``` ## 选项 ### A ```cpp class Solution { public: bool canFinish(int numCourses, vector> &prerequisites) { if (numCourses == 0) return true; vector course(numCourses, 0); vector> rela(numCourses); for (int i = 0; i < prerequisites.size(); i++) { course[prerequisites[i][0]]++; rela[prerequisites[i][1]].push_back(prerequisites[i][0]); } queue cq; for (int i = 0; i < numCourses; i++) if (course[i] == 0) cq.push(i); int num = numCourses; while (!cq.empty()) { int c = cq.front(); cq.pop(); num--; for (int i = 0; i < rela[c].size(); i++) { int this_course = rela[c][i]; course[this_course]--; if (course[this_course] == 0) cq.push(this_course); } } return num == 0 ? true : false; } }; ``` ### B ```cpp class Solution { public: bool canFinish(int numCourses, vector> &prerequisites) { vector res, degree(numCourses, 0); unordered_map> map; for (auto &e : prerequisites) { map[e[1]].push_back(e[0]); degree[e[0]]++; } queue q; for (int i = 0; i < numCourses; i++) { if (degree[i] == 0) { q.push(i); } } while (!q.empty()) { int cur = q.front(); res.push_back(cur); q.pop(); for (auto &next : map[cur]) { degree[next]--; if (degree[next] == 0) q.push(next); } } return res.size() == numCourses; } }; ``` ### C ```cpp class Solution { public: bool canFinish(int numCourses, vector> &prerequisites) { vector> graph(numCourses, vector()); for (auto num : prerequisites) { graph[num[1]].push_back(num[0]); } vector visit(numCourses); for (int i = 0; i < numCourses; i++) { if (!dfs(graph, visit, i)) return false; } return true; } bool dfs(vector> &g, vector &visit, int i) { if (visit[i] == -1) return false; if (visit[i] == 1) return true; visit[i] = -1; for (auto a : g[i]) { if (!dfs(g, visit, a)) return false; } visit[i] = 1; return true; } }; ```