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. 【转】JDK5.0中JVM堆模型、GC垃圾收集详细解析

    基本概念 堆/Heap JVM管理的内存叫堆:在32Bit操作系统上有4G的限制,一般来说Windows下为2G,而Linux下为3G:64Bit的就没有这个限制.JVM初始分配的内存由-Xms指定, ...

  2. C字符贪吃蛇

    算法参照Perl字符贪吃蛇,源码: #include <stdio.h> #include <windows.h> #define WIDTH 12 // 宽 #define ...

  3. 关于python 的参数的汇总转

    来自:https://blog.csdn.net/gaotihong/article/details/95687282 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附 ...

  4. centos7使用MariaDB(转)

    转载文章:https://blog.csdn.net/zwkkkk1/article/details/78444581?locationNum=10&fps=1 最近使用centos7,php ...

  5. Unity 2018 Game Development in 24 Hours Sams Teach Yourself 3rd Edition

    最新的Unity2018自学教材,如果新入坑Unity,直接从2018开始看吧! 点击下载提取码:tngj

  6. pip错误 ImportError: No module named 'pip_internal'

    ubuntu16.04 - wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate sudo python get-pip.p ...

  7. restful swagger api java go

    rest框架使用swagger api,接口越来越多后性能会很差,如何优化? - 知乎https://www.zhihu.com/question/59349319 golang restful 框架 ...

  8. SIEBEL BIP报表平台端口被占用

    管理-服务器配置S 找到对应的服务名称: CRMSS2 组件输入:XMLP Report Server 选择[高级]后,查找 Static Port Number 即可修改端口号. 端口号可查询测试环 ...

  9. C++构造函数以及何时被调用

    using namespace std; class A { public: A() { cout << "默认无参构造函数" << endl; } #if ...

  10. 123457123456#2#----com.MC.HuiHuaGame33--前拼后广--画画填色Game-mc

    com.MC.HuiHuaGame33--前拼后广--画画填色Game-mc