1、题目描述
你这个学期必须选修 numCourse 门课程,记为 0 到 numCourse-1 。
在选修某些课程之前需要一些先修课程。 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们:[0,1]
给定课程总量以及它们的先决条件,请你判断是否可能完成所有课程的学习?
示例 1:
输入: 2, [[1,0]]
输出: true
解释: 总共有 2 门课程。学习课程 1 之前,你需要完成课程 0。所以这是可能的。
2、题目分析
-
修一门课之前需要满足这门课的先修课程已经修完
-
每次先修完哪些课程呢?第一个肯定是没有先修课程的那个,之后选出先修课程已修完的
-
如何表示课程之间的关系呢?使用有向图,[0,1]表示0的先修课程是1,那么1->0就表示这俩的关系,0有个入度为1
-
如果图中有环则该环的节点都不能取,只有当节点的入度==0的时候才能取这个点,之后相关的节点入度-1
-
拓扑排序:对DAG的顶点进行排序,使得每条有向边(u,v),在排序记录中u比v先出现
1、统计每个节点的入度生成入度表indegrees
2、将所有入度为0的点入队,队列非空时依次出队
--将此节点pre的所有邻接节点的入度-1,indegrees[cur] -= 1;
--入度=0的节点入队
--每当由pre节点出队时,numCourses–