LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似。

注意到。在for (auto p: prerequistites)中特判了输入中可能出现的平行边或自环。

代码:

class Solution
{
public:
vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites)
{
// [0, {1, 2, 3}], means after finishing #0, you "might" be able to take #1, #2, #3
// That is, you must finish #0, before trying to take #1, #2, #3
map<int, vector<int>> course_chain;
vector<int> in_degree(numCourses, 0);
queue<int> q;
vector<int> ret; for (auto p: prerequisites)
{
// self-loop, return empty vector.
if (p.first == p.second)
{
return vector<int>();
}
// no duplicate edges input
if (find(course_chain[p.second].begin(), course_chain[p.second].end(), p.first) == course_chain[p.second].end())
{
course_chain[p.second].push_back(p.first);
++ in_degree[p.first];
}
}
for (size_t i = 0; i < numCourses; ++ i)
{
if (in_degree[i] == 0)
{
q.push(i);
}
}
for (; !q.empty(); q.pop())
{
int pre_course = q.front();
ret.push_back(pre_course);
for (auto it = course_chain[pre_course].begin(); it != course_chain[pre_course].end(); ++ it)
{
-- in_degree[*it];
if (in_degree[*it] == 0)
{
q.push(*it);
}
}
} return ret.size()==numCourses? ret: vector<int>();
}
};

LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)的更多相关文章

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

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

  2. [leetcode]210. Course Schedule II课程表II

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

  3. [LeetCode] 210. Course Schedule II 课程安排II

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

  4. Java for LeetCode 210 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 ...

  5. (medium)LeetCode 210.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 ...

  6. LeetCode 207. Course Schedule(拓扑排序)

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

  7. Leetcode 210 Course Schedule II

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

  8. 【LeetCode】210. Course Schedule II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 拓扑排序,BFS 拓扑排序,DFS 参考资料 日期 ...

  9. 【刷题-LeetCode】210. Course Schedule II

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

随机推荐

  1. QQ 临时会话+图标 HTML代码

    啦啦啦 QQ会话的HTML代码 <a target="_blank" href="http://wpa.qq.com/msgrd?v=3& uin=2553 ...

  2. 团队作业-Beta版本发布

    这个作业属于哪个课程  <课程的链接>            这个作业要求在哪里 <作业要求的链接> 团队名称 Three cobblers 这个作业的目标 Beta版本发布报 ...

  3. Unity引擎GUI之Image

    UGUI的Image等价于NGUI的Sprite组件,用于显示图片. 一.Image组件: Source Image(图像源):纹理格式为Sprite(2D and UI)的图片资源(导入图片后选择T ...

  4. STL之map篇

    度熊所居住的 D 国,是一个完全尊重人权的国度.以至于这个国家的所有人命名自己的名字都非常奇怪.一个人的名字由若干个字符组成,同样的,这些字符的全排列的结果中的每一个字符串,也都是这个人的名字.例如, ...

  5. Percona Xtrabackup导出/导入单表

    默认情况下,InnoDB表不能通过直接复制表文件的方式在mysql服务器之间进行移植,即便使用了innodb_file_per_table选项.而使用Xtrabackup工具可以实现此种功能,不过,此 ...

  6. ICCV2015上的GazeTracker论文总结

    SLAM问题先慢慢编译一段时间,赶紧拾起来GazeTrack的事情...... ICCV2015的大部分paper已经可以下载,文章列表在这个位置. http://www.cvpapers.com/i ...

  7. android studio: 为现有项目添加C++支持

    刚开始创建项目的时候并没有勾选“include C++ support” 选项: 后期增加步骤: 1.拷贝已有支持C++项目的CMakeLists.txt文件到现有项目的app目录下: 2.在app/ ...

  8. Android 性能测试初探(四)

    书接上文 Android 性能测试初探(三) 自从 cpu及内存后,GPU 这个词对于 PC 性能测试者也不陌生了,什么 3Dmax,安兔兔之类的第三方软件让 GPU 在移动端性能测试领域都知晓,但对 ...

  9. hadoop中的job.setOutputKeyClass与job.setMapOutputKeyClass

    初学mr时,觉得没什么,但是学了一段时间,重新复习时发现程序中mr程序中一般都会有 hadoop中的job.setOutputKeyClass(theClass)与job.setOutputValue ...

  10. 定位IO瓶颈的方法,iowait低,IO就没有到瓶颈?

    通过分析mpstat的iowait和iostat的util%,判断IO瓶颈 IO瓶颈往往是我们可能会忽略的地方(我们常会看top.free.netstat等等,但经常会忽略IO的负载情况),今天给大家 ...