There are a total of n courses you have to take, labeled from `0` to `n - 1`.

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: `[0,1]`

Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?

For example:

`2, [[1,0]]`

There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.

`2, [[1,0],[0,1]]`

There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.

Hints:
1. This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
2. There are several ways to represent a graph. For example, the input prerequisites is a graph represented by a list of edges. Is this graph representation appropriate?
3. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
4. Topological sort could also be done via BFS.

```class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<));
vector<);
for (auto a : prerequisites) {
graph[a[]].push_back(a[]);
++]];
}
queue<int> q;
; i < numCourses; ++i) {
) q.push(i);
}
while (!q.empty()) {
int t = q.front();
q.pop();
for (auto a : graph[t]) {
--in[a];
) q.push(a);
}
}
; i < numCourses; ++i) {
) return false;
}
return true;
}
};```

```class Solution {
public:
bool canFinish(int numCourses, vector<vector<int> >& prerequisites) {
vector<vector<));
vector<);
for (auto a : prerequisites) {
graph[a[]].push_back(a[]);
}
; i < numCourses; ++i) {
if (!canFinishDFS(graph, visit, i)) return false;
}
return true;
}
bool canFinishDFS(vector<vector<int> > &graph, vector<int> &visit, int i) {
) return false;
) return true;
visit[i] = -;
for (auto a : graph[i]) {
if (!canFinishDFS(graph, visit, a)) return false;
}
visit[i] = ;
return true;
}
};```

Minimum Height Trees

Course Schedule II

http://www.cnblogs.com/easonliu/p/4483437.html

https://leetcode.com/discuss/34741/python-20-lines-dfs-solution-sharing-with-explanation

LeetCode All in One 题目讲解汇总(持续更新中...)

## [LeetCode] Course Schedule 课程清单的更多相关文章

1. [LeetCode] Course Schedule II 课程清单之二

There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

2. [LeetCode] Course Schedule III 课程清单之三

There are n different online courses numbered from 1 to n. Each course has some duration(course leng ...

3. LeetCode Course Schedule II

原题链接在这里:https://leetcode.com/problems/course-schedule-ii/ 题目: There are a total of n courses you hav ...

4. [LeetCode] Course Schedule I (207) &amp; II (210) 解题思路

207. Course Schedule There are a total of n courses you have to take, labeled from 0 to n - 1. Some ...

5. LeetCode - Course Schedule 解题报告

以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

6. [Leetcode] Course Schedule

There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

7. [LeetCode] Minimum Height Trees 最小高度树

For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

8. [CareerCup] 4.2 Route between Two Nodes in Directed Graph 有向图中两点的路径

4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nod ...

9. 01_Python 基础课程安排

Python 基础课程安排 目标 明确基础班课程内容 课程清单 序号 内容 目标 01 Linux 基础 让大家对 Ubuntu 的使用从很 陌生 达到 灵活操作 02 Python 基础 涵盖 Py ...

## 随机推荐

1. 玩转JavaScript OOP[0]&mdash;&mdash;基础类型

前言 long long ago,大家普遍地认为JavaScript就是做一些网页特效的.处理一些事件的.我身边有一些老顽固的.NET程序员仍然停留在这种认知上,他们觉得没有后端开发肯定是构建不了系统 ...

3. 【项目经验】之——Controller向View传值

我们的ITOO进行了一大部分了,整体上来说还是比较顺利的.昨天进行了一次验收,大体上来说,我们新生这块还是可以的.不仅仅进行了学术上的交流,还进行了需求上的更新.也正是由于这一次,我有了解到了一个新的 ...

4. css reset及部分layout样式

/*! normalize.css v3.0.3 | MIT License | github.com/necolas/normalize.css */html { font-family: sans ...

5. SSM框架整合（ Spring 、 SpringMVC 和 Mybatis )

1.基本概念 1.1.Spring Spring 是一个开源框架, Spring 是于 2003  年兴起的一个轻量级的 Java  开发框架,由 Rod Johnson  在其著作 Expert O ...

6. Lua中使用table实现的其它5种数据结构

Lua中使用table实现的其它5种数据结构 lua中的table不是一种简单的数据结构,它可以作为其他数据结构的基础,如:数组,记录,链表,队列等都可以用它来表示. 1.数组 在lua中,table ...

7. response与request回顾学习

一.response response是servlet.service方法的一个参数,它的类型是javax.servlet.http.HttpServletResponse,在客户端每发出一个请求时, ...