There are a total of n courses you have to take, labeled from
0
ton - 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.
Note:
- The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
- You may assume that there are no duplicate edges in the input prerequisites.
题目解析:该题的意思是,第一个数字告诉你有n个顶点,后面的二位数组则是告诉你两个端点的前后置关系
后一个数是前一个数的前置条件
要想能够实现遍历所有的课程,不能包含回路,若有回路,则这个回路所有的都不能遍历。如果没有回路,采用DFS
2017算法课.14(Course Schedule)
最新推荐文章于 2022-03-09 12:11:37 发布