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:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.

click to show more hints.

Hints:
    1. This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
    2. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
    3. Topological sort could also be done via BFS.

本题最终转化为求有向图中是否有存在环,转化为拓扑序问题

class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<vector<int>> graph(numCourses);
vector<int> in_degree(numCourses, ); for (auto p : prerequisites) {
graph[p.second].push_back(p.first);
in_degree[p.first]++;
} queue<int> q;
int cnt = ;
for (int i = ; i < numCourses; i++) {
if (in_degree[i] == )
q.push(i);
}
while (!q.empty()) {
int cur = q.front();
q.pop();
for (auto it = graph[cur].begin(); it != graph[cur].end(); it++) {
if (--in_degree[*it] == )
q.push(*it);
}
} for (int i = ; i < numCourses; i++) {
if (in_degree[i] != )
return false;
}
return true;
}
};

Course Schedule的更多相关文章

  1. [LeetCode] 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 ...

  2. [LeetCode] Course Schedule 课程清单

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

  3. POJ 1325 Machine Schedule——S.B.S.

    Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13731   Accepted: 5873 ...

  4. Spring Schedule 任务调度实现

    我们都知道任务调度可以用Quartz,但对于简单的定时任务,可以直接用Spring内置的Schedule来实现.可以由两种方式,注释+XML配置 注解方式: 注解也要先在sping.xml配置文件中配 ...

  5. HDU 3572 Task Schedule(拆点+最大流dinic)

    Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  6. Spring Shedule Task之注解实现 (两次启动Schedule Task 的解决方案)

    在spring 中的新引入的task 命名空间.可以部分取代 quartz 功能,配置和API更加简单,并且支持注解方式. 第一步: 在Spring的相关配置文件中(applicationContex ...

  7. Schedule 学习

    现在做的项目都有用到Schedule,现在用一点时间来总结. 一.首先要到Nuget中下载Quartz.net. 二.下载下来了,你需要对它进行配置,使它能给你正常的使用. 三.在Global.asa ...

  8. Spring 4.x Task 和 Schedule 概述(代java配置)

    转载请注明https://zhangzhaoyu.github.io/2016/09/30/spring-task-and-schedule-deep-research/ 摘要 在很多业务场景中,系统 ...

  9. OpenMP并行构造的schedule子句详解 (转载)

    原文:http://blog.csdn.net/gengshenghong/article/details/7000979 schedule的语法为: schedule(kind, [chunk_si ...

  10. [nRF51822] 4、 图解nRF51 SDK中的Schedule handling library 和Timer library

    :nRF51822虽然是一个小型的单片机,但是能真正达到任意调用其官方驱动以及BLE协议栈的人还是奇缺的.据我所见,大都拿官方给的一个冗长的蓝牙低功耗心率计工程改的.之前我对于这个工程进行log跟踪, ...

随机推荐

  1. cacti监控juniper路由器

    之前也没有写过博客,但是最近一直在做监控.从网上查找很多资料都还是感觉差一点.所以自己添加一份我在cacti监控路由器的步骤. 环境,ubuntu14.04,apt-get install cacti ...

  2. Node与express开发

    1.初识Express Express 网站上是这样介绍 Express 的: "精简的.灵活的 Node.js Web 程序框架,为构建单页.多页及混合的 Web 程序提供了一系列健壮的功 ...

  3. 【工具】【电子设计】超屌的 fritzing 新建元件

    fritzing 有多好,用了才知道,但是通常会遇到一个问题,他的元件库不一定够用,这时候就得自己做元件了,但是搜了一下网上没有相关的教程啊. 算了,去官网看英文吧.. 首先在最新版本不支持直接新建元 ...

  4. 转: Hibernate commit() 和flush() 的区别

    [java] view plaincopyprint? <<精通Hibernate java对象持久化技术详解>> ,flush()方法进行清理缓存的操作,执行一系列的SQL语 ...

  5. E. Vasya and Beautiful Arrays

    http://codeforces.com/contest/355/problem/E 每个数都可以变成段 [a-k,a], 某一个因子是否被所有的段包含,就是把这个因子以及它的所有倍数看成点, 看是 ...

  6. Apriori on MapReduce

    Apiroi算法在Hadoop MapReduce上的实现 输入格式: 一行为一个Bucket 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 34 36 38 ...

  7. swift 自定义图片轮播视图

    Swift封装图片轮播视图: import UIKit class XHAdLoopView: UIView { private var pageControl : UIPageControl? pr ...

  8. php 常用数组操作

    php常用的数组操作函数,包括数组的赋值.拆分.合并.计算.添加.删除.查询.判断.排序等 array_combine 功能:用一个数组的值作为新数组的键名,另一个数组的值作为新数组的值 <?p ...

  9. ORACLE删除当前用户下所有的表的方法

    1.如果有删除用户的权限,则可以: drop user user_name cascade; 加了cascade就可以把用户连带的数据全部删掉. 删除后再创建该用户. --创建管理员用户 create ...

  10. 基于Jenkins + Git的PHP项目编译脚本

    本文针对的是了解或已经在使用Jenkins和Git的开发者或团队. 本团队使用了Jenkins作为持续集成平台,Git作为版本管理工具,而本人负责的项目是PHP项目,所谓发布项目就是复制文件. 通常有 ...