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. HashMap & HashTable的区别

    HashMap & HashTable的区别主要有以下: 1.HashMap是线程不安全的,HashTable是线程安全的.由这点区别可以知道,不考虑线程安全的情况下使用HashMap的效率明 ...

  2. 关于C++构造函数初始化顺序

    这里主要是说序列初始化成员变量时,存在这样的规则: 1. 先进行序列初始化,再进行构造函数函数体内的赋值等操作. 2. 序列初始化,不是简单的自左至右或自右至左,而是根据成员变量的定义顺序来初始化. ...

  3. Can't update: no tracked branch

    git更新错误:Can't update: no tracked branch No tracked branch configured for branch master. To make your ...

  4. String.format介绍

    java类中提供的一种方法:String.format(String format, Object ... args) 提供字符串格式化功能: 不同转换符实现不同数据类型到字符串的转换: %s--字符 ...

  5. selenium+python环境搭建

    1.安装python-2.7.3.msi 2.安装pywin32-216.win32-py2.7.exe 3.下selenium包,selenium-2.35.0.tar.gz,放到D:\autote ...

  6. 常用CSS样式

    1.line-height:行高.默认normal normal:允许内容顶开或溢出制定的容器边界; length:15px,可以为负数; ... 2.overflow:滚动条设置 overflow- ...

  7. 循序渐进redis(一) —— redis的安装及可视化工具的使用

    1.安装 注意事项: 1.安装gcc 2.编译带参数: make MALLOC=libc 2.可视化客户端工具 推荐使用RedisClient,提供了基本的CRUD以及过期设置等操作的图形化接口,在项 ...

  8. Multivariance Linear Regression练习

    %% 方法一:梯度下降法 x = load('E:\workstation\data\ex3x.dat'); y = load('E:\workstation\data\ex3y.dat'); x = ...

  9. Unity中游戏的声音管理

    using UnityEngine;using System.Collections;using System.Collections.Generic;/// <summary>/// 用 ...

  10. UE4蓝图编程的第一步

    认识UE4蓝图中颜色与变量类型: UE4中各个颜色对应着不同的变量,连接点和连线的颜色都在表示此处是什么类型的变量.对于初学者来说一开始看到那么多连接点, 可能会很茫然,搞不清还怎么连,如果知道了颜色 ...