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.

Note:

  1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
  2. You may assume that there are no duplicate edges in the input prerequisites.
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<vector<int>> graph(numCourses, vector<int>());
vector<int> in(numCourses, );
for (auto a : prerequisites) {
graph[a.second].push_back(a.first);
++in[a.first];
}
queue<int> q;
for (int i = ; i < numCourses; ++i) {
if (in[i] == ) q.push(i);
}
while (!q.empty()) {
int t = q.front();
q.pop();
for (auto a : graph[t]) {
--in[a];
if (in[a] == ) q.push(a);
}
}
for (int i = ; i < numCourses; ++i) {
if (in[i] != ) return false;
}
return true;
}
};

Topological Sor-207. Course Schedule的更多相关文章

  1. LeetCode - 207. Course Schedule

    207. Course Schedule Problem's Link ---------------------------------------------------------------- ...

  2. 207. Course Schedule

    https://blog.csdn.net/wongleetion/article/details/79433101 问题的实质就是判断一个有向图是否有环,利用入度去解决这个问题 使用bfs解决问题. ...

  3. LN : leetcode 207 Course Schedule

    lc 207 Course Schedule 207 Course Schedule There are a total of n courses you have to take, labeled ...

  4. 11/11 <Topological Sort> 207

    207. Course Schedule 我们定义二维数组 graph 来表示这个有向图,一维数组 in 来表示每个顶点的入度.我们开始先根据输入来建立这个有向图,并将入度数组也初始化好.然后我们定义 ...

  5. [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 prereq ...

  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 prer ...

  7. Java for LeetCode 207 Course Schedule【Medium】

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

  8. (medium)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 prer ...

  9. 207. Course Schedule(Graph; BFS)

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

  10. 【LeetCode】207. Course Schedule (2 solutions)

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

随机推荐

  1. Golang实现一个密码生成器

    小地鼠防止有人偷他的果实,在家里上了一把锁.这个锁怎么来的呢?请往下看.. package main import ( "flag" "fmt" "m ...

  2. Judy Array API介绍

    本文介绍https://code.google.com/p/judyarray/这个JudyArray实现的API. judy_open:新建一个JudyArray,并返回指向这个JudyArray的 ...

  3. 开发中常遇到的Python陷阱和注意点-乾颐堂

    最近使用Python的过程中遇到了一些坑,例如用datetime.datetime.now()这个可变对象作为函数的默认参数,模块循环依赖等等. 在此记录一下,方便以后查询和补充. 避免可变对象作为默 ...

  4. Mybatis传值为空需要配置JdbcType来解决吗?(XML文件不需要配置JdbcType)

    1,解决思路,配置自定义的语言驱动,重写自己的Paramethander package cn.com.servyou.gxdqy.tool.xmlhelper; import org.apache. ...

  5. 2018.09.27 bzoj2118: 墨墨的等式(最短路+背包)

    传送门 好题啊. 首先找到最小的一个非零系数记做a1a_1a1​,然后如果WWW modmodmod a1=W′a_1=W'a1​=W′ modmodmod a1a_1a1​,且WWW是方程的一个可行 ...

  6. sql笔试练习

    转:http://www.360doc.com/content/16/0919/17/14804661_592046675.shtml 本文是在Cat Qi的参考原帖的基础之上经本人一题一题练习后编辑 ...

  7. python 修改文件编码方式

    import chardet import os def strJudgeCode(str): return chardet.detect(str) def readFile(path): try: ...

  8. web.xml 404 500 配置

    web.xml <error-page> <error-code>404</error-code> <location>/error404.html&l ...

  9. 关于RabbitMQ一点

    RabbitMQ是AMQP(高级消息队列协议)的标准实现,理论上可以保证消息发送的准确性 RabbitMQ是用Erlang语言编写的,而Erlang语言具有以下特点: 并发性--Erlang支持超大量 ...

  10. 自己动手绘制ToolBar

    VC MFC工具栏(CToolBar)控件 工具栏 工具栏控件在控件面板里没有对应的选项(图标),但有一个工具栏控件类CToolBar,所以我们如果要创建一个工具栏控件并显示在窗口里的话,只能用代码来 ...