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. mvc EF 从数据库更新实体,添加视图实体时添加不上的问题

    视图对象没有一列为非null的,解决办法,在视图中,将某一列排除为null的可能,比如:isnull(te,1),即可.

  2. Golang之hello,beego

    学习谢大神的beego记录 过程: 目录结构: 编译命令: go build -o myBeego.exe go_dev/day13/beego_example/main执行myBeego.exe即可 ...

  3. Eclipse Gradle配置

    一.Gradle简介 Gradle 是以 Groovy 语言为基础,面向Java应用为主.基于DSL(领域特定语言)语法的自动化构建工具. 二.配置步骤如下: 1.资源下载: Grandle官网下载G ...

  4. 12月6日 被引入的jsp 页面,引入 js 要注意结束符 要用 </script> 而不是 />

    12月6日  被引入的jsp 页面,引入 js 要注意结束符 要用  </script> 而不是 />

  5. ORACLE 查看分区表分区大小

    SELECT *  FROM dba_segments t WHERE t.segment_name ='table_name'; pratition_name : 分区名 bytes : 分区大小( ...

  6. ajax序列化表单,再也不用通过data去一个个的传值了

    jQuery的serialize()方法通过序列化表单值,创建URL编码文本字符串,我们就可以选择一个或多个表单元素,也可以直接选择form将其序列化 这样,我们就可以把序列化的值传给ajax()作为 ...

  7. UVa 11992 Fast Matrix Operations (线段树,区间修改)

    题意:给出一个row*col的全0矩阵,有三种操作 1 x1 y1 x2 y2 v:将x1 <= row <= x2, y1 <= col <= y2里面的点全部增加v: 2 ...

  8. Java_得到GET和POST请求URL和参数列表

    一. 获取URL: getRequestURL()(还有个getRequestURI(),只取后面部分) 二. 获取参数列表: 1.getQueryString() 只适用于GET,比如客户端发送ht ...

  9. Spring3.x错误----NotFoundException: org.objectweb.asm.codevisitor

    Spring3.x错误: 解决办法: 一定要引入cglib-nodep-2.1_3.jar,而不是cglib-2.1.3.jar

  10. C - 无间道之并查集 HihoCoder - 1066

    输入 每个测试点(输入文件)有且仅有一组测试数据. 每组测试数据的第1行为一个整数N,表示黑叔叔总共进行的操作次数. 每组测试数据的第2~N+1行,每行分别描述黑叔叔的一次操作,其中第i+1行为一个整 ...