class Solution {
public:
static bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
//初始化入度
vector<int> indegree(numCourses, );
//初始化邻接表
vector<vector<int>> g(numCourses, vector<int>());
for (int i = ; i < prerequisites.size(); i++) {
int first = prerequisites.at(i).first;
int second = prerequisites.at(i).second;
g[second].push_back(first);
indegree[first] ++;
}
queue<int> que;
for (int i = ; i < numCourses; i++) {
if (indegree.at(i) == ) {
que.push(i);
}
}
while(!que.empty()) {
int u = que.front();
que.pop();
for (auto v : g[u]) {
indegree[v] --;
if (indegree.at(v) == ) {
que.push(v);
}
}
} for (auto v : indegree) {
if (v != ) {
return false;
}
}
return true;
}
};

【leecode】 Course Schedule的更多相关文章

  1. 【leetcode】Course Schedule(middle)☆

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

  2. 【最大流】【HDU3572】Task Schedule

    题意: 有N个事件,M台机器.事件有开始时间,持续时间,要在结束时间之前完成,问是否能完成所有事件? 非自己思考出来的 建图:把每个任务和每一天都看做一个点,添加源点和汇点.源点与每个任务之间连一条边 ...

  3. 【Leecode】两数相加

    学习了链表结构,链表中元素的访问,对象指针的初始化与赋值,链表的创建(多个节点链接起来),进位计算的表达. 100ms /** * Definition for singly-linked list. ...

  4. 【Leecode】两数之和

    学习使用标准模板库(STL)中的map,hash_map.涉及数据结构知识:哈希表,红黑树. map的使用方法 https://www.cnblogs.com/fnlingnzb-learner/p/ ...

  5. 【UVA1194】Machine Schedule

    题目大意:给定 N 个任务和两台机器,每个任务可以在任意一台机器上执行,每台机器有 N 个启动状态,不同任务需要机器在不同的状态下执行,求执行所有任务需要多少个不同的状态. 题解:由于一个任务一定要被 ...

  6. 【贪心】hdu6180 Schedule

    题意:给你n个任务的开始时间和结束时间,一个机器同时最多执行一个任务,问你最少要几个机器.保证机器最少的前提下,问你每个机器的开动时间(最后一次关闭-第一次开启)之和最少是多少. 把这些线段画在数轴上 ...

  7. 【Poj1325】Machine Schedule机器调度

    目录 List Description Input Output Sample Input Sample Output HINT Solution Code Position: http://poj. ...

  8. 【leecode】小练习(简单8题)

    def twoSum(nums, target): """ 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[ ...

  9. 【leecode】独特的电子邮件地址

    每封电子邮件都由一个本地名称和一个域名组成,以 @ 符号分隔. 例如,在 alice@leetcode.com中, alice 是本地名称,而 leetcode.com 是域名. 除了小写字母,这些电 ...

随机推荐

  1. ORACLE数据库特性

    目录 ORACLE数据库特性 一.学习路径 二.ORACLE的进程情况 三.ORACLE服务器的启动和关闭 (SQLPLUS环境挂起和恢复等) 连接Oracle的几种方式 四.几个关注点 1. ORA ...

  2. Dynamic Filter Networks

    Dynamic Filter Networks 2019-06-10 11:29:19 Paper:http://papers.nips.cc/paper/6578-dynamic-filter-ne ...

  3. [Java/Reflect]使用反射机制获得一个对象的属性名和属性值

    一个辅助对象,用于给属性排序 class KeyValue implements Comparable<KeyValue>{ String key; Object value; @Over ...

  4. 前端性能测试工具 : dynaTrace Ajax (还没写完)

    今天开始写这个工具, #什么是dynaTrace Ajax? 随着 jQuery.Dojo.YUI 等框架的兴起让构建 Web2.0 应用更加容易,但随之带来的定位等应用问题也越来越难,尤其是与性能相 ...

  5. Diffie-Hellman算法简介

    一.DH算法是一种密钥交换协议,它可以让双方在不泄漏密钥的情况下协商出一个密钥来. DH算法基于数学原理,比如小明和小红想要协商一个密钥,可以这么做: . 小明先选一个素数和一个底数,例如,素数p=, ...

  6. 关于golang中IO相关的Buffer类浅析

    io重要的接口 在介绍buffer之前,先来认识两个重要的接口,如下边所示: type Reader interface { Read(p []byte) (n int, err error) } t ...

  7. ISO/IEC 9899:2011 条款6.9.1——函数定义

    6.9.1 函数定义 语法 1.function-definition: declaration-specifiers    declarator    declaration-listopt     ...

  8. Qt QLabel加载图片

    QLabel加载图片 //在对应的控件中显示图片 void qm_img::DisplayImg(cv::Mat imgParam, QLabel *labelParam) { if (!imgPar ...

  9. MongoDB数据表添加字段

    db.tshare_a.insert( { "_id" : ObjectId("57172b0f657f8bbb34d70147"), "picUrl ...

  10. QTableView加载数据

    void VCAdmin::searchAllUser() { strID_Index = ""; if (NULL == vcManageDatabaseObj) { vc_ad ...