题目描述

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, return the ordering of courses you should take to finish all courses.

There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.

题目大意

题意同LeetCode-207 Course Schedule,但需要返回看书的顺序(如果存在多个解,则返回其中之一)。

示例

E1

Input: 2, [[1,0]]
Output: [0,1]

E2

Input: 4, [[1,0],[2,0],[3,1],[3,2]]
Output: [0,1,2,3] or [0,2,1,3]

解题思路

依然使用DFS来遍历所有可能性,但需要保存可能的返回结果,算法中需要使用两个数组来分别保存图节点的访问情况,其中第一个数组表示本次以K为源节点进行DFS遍历时所访问的节点,第二个数组表示在所有的访问情况中结点的访问状态。(PS:语言表达看似很复杂,实际看到代码就应该很容易理解)

复杂度分析

时间复杂度:O(|V| + |E|)

空间复杂度:O(|V| + |E|)

代码

class Solution {
public:vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {vector<unordered_set<int> > edge(numCourses);vector<int> res;//对图的边进行二维数组保存for(int i = 0; i < prerequisites.size(); ++i) {edge[prerequisites[i][0]].insert(prerequisites[i][1]);}//表示以当前访问的结点为源点进行dfs时其余结点的访问情况vector<bool> todo(numCourses, false);//表示在所有结点访问的过程中的结点被访问情况vector<bool> done(numCourses, false);for(int i = 0; i < numCourses; ++i) {if(!done[i] && !dfs(res, edge, todo, done, i)) {return {};}}return res;}bool dfs(vector<int>& res, vector<unordered_set<int> >& edge, vector<bool>& todo, vector<bool>& done, int k) {//若该节点在本次DFS过程中以被访问,并且本次还要访问,则代表产生环if(todo[k])return false;//若该结点已被完全访问,其子节点也完全被遍历过,则表示该结点为终结点if(done[k])return true;todo[k] = done[k] = true;//DFS遍历结点K的子节点for(unordered_set<int>::iterator iter = edge[k].begin(); iter != edge[k].end(); ++iter) {if(!dfs(res, edge, todo, done, *iter))return false;}//若满足以上条件,则代表在这个小的以K为源点的遍历过程中,无环产生
        res.push_back(k);//为了不影响其他DFS过程中结点的访问情况,将该位置的访问情况进行还原todo[k] = false;return true;}
};

转载于:https://www.cnblogs.com/heyn1/p/11048172.html

LeetCode-210 Course Schedule II相关推荐

  1. leetcode 210. Course Schedule II | 210. 课程表 II(Java)

    题目 https://leetcode.com/problems/course-schedule-ii/ 题解 本题与 leetcode 207. Course Schedule 基本相同,代码只需要 ...

  2. 从LeetCode 210. Course Schedule II 了解拓扑排序

    问题简述 给定n节课,每节课按0~n-1编号. 在修某些课的时候需要有其它课的基础,必须先上先修课.现在用pair的形式来表示要先修的课,比如 [ [0,1], [1,2] ] 就表示在修课程1之前必 ...

  3. 207. Course Schedule 210. Course Schedule II

    拓扑排序的问题 套路: 1. 初始化所有的节点的入度为零. 2. 遍历给定的输入要求. 维护一个indegree 的表.记录每个节点的入度. 维护map, 每个节点的后续节点 3. 得到这两个表以后开 ...

  4. leetcode Course Schedule II

    题目连接 https://leetcode.com/problems/course-schedule-ii/ Course Schedule II Description There are a to ...

  5. [Leedcode][JAVA][第210 题][课程表 II][拓扑排序][BFS][DFS][有向图]

    [问题描述][第210 题][课程表 II][中等] 现在你总共有 n 门课需要选,记为 0 到 n-1.在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用 ...

  6. Leetcode - Subsets I,II

    Leetcode - 078. Subsets 这道题重定义了什么叫可行解: 一般而言,可行解需要满足强约束性条件集,而本题的可行解就是单一弱约束性条件(distinct integers,只需要当前 ...

  7. Leetcode - Permutations I,II

    Leetcode - 046 Permutations 全排列问题是回溯的典型例题: 1.可行解的组成形式是给定数组中的所有数的组合,故而大小上可以作为可行解判定条件 2.每次需要在剩下可被选中的集合 ...

  8. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  9. 【DFS + 记忆化递归】LeetCode 140. Word Break II

    LeetCode 140. Word Break II Solution1:我的答案 纯DFS,在第31个case时超时,还是记录一下.. class Solution { // DFS public ...

  10. 【重点!DP】LeetCode 639. Decode Ways II

    LeetCode 639. Decode Ways II 参考网址:https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-639-d ...

最新文章

  1. GO语言教程4:defer(延迟语句)详解
  2. iOS:极光推送控制器跳转
  3. 学python的基础-老司机学python篇:第一季(基础速过、机器学习入门)
  4. 为什么人们爱听有声书?
  5. 支付宝2018年账单发布,更注重用户隐私保护
  6. zookeeper集群搭建配置zoo.cnf
  7. java动态代理_Java 动态代理和依赖注入
  8. 沃尔沃召回部分进口及国产车 共计131591辆
  9. VBA代码执行过程中,显示程序的运行状态
  10. [转]隐马尔可夫模型
  11. 新来的妹纸问我,如果把几百万数据放入内存,会不会把系统撑爆?
  12. jquery实现网易云音乐的歌词展示部分
  13. abb880/580驱动程序,zmu程序图纸571/592/792需要
  14. allegro转AD教程
  15. grads插值_GrADS第6章变量和函数讲课.ppt
  16. 【加拿大签证】加拿大工签/工作许可work permit 办理流程【官方详细完整,加拿大访问学者必看】
  17. python写酒店管理系统_基于WEB的小型酒店管理系统,源码下载
  18. Rust学习笔记(14)——struct、Option和Box组合应用实现单向链表之二
  19. alv 导出料号缺少最后一位
  20. 黑衣人---走出软件作坊:三五个人十来条枪 如何成为开发正规军(三十四)

热门文章

  1. Energetically Consistent Invertible Elasticity
  2. 知识图谱组队学习Task02——项目介绍
  3. Hive已死,SparkSQL来啦~
  4. python中float输入文字_为什么Python的float对于一些很长的输入会引发ValueError?
  5. The Past Mistake is the Future Wisdom: Error-driven ContrastiveProbability Optimization for Chinese
  6. Error: cannot allocate vector of size 88.1 Mb问题
  7. 多个notification引发的问题
  8. Hbuilder 移动app
  9. 5.Git使用详细教程
  10. 手机号码替换中间几位为*号