【leecode】 Course Schedule
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的更多相关文章
- 【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 ...
- 【最大流】【HDU3572】Task Schedule
题意: 有N个事件,M台机器.事件有开始时间,持续时间,要在结束时间之前完成,问是否能完成所有事件? 非自己思考出来的 建图:把每个任务和每一天都看做一个点,添加源点和汇点.源点与每个任务之间连一条边 ...
- 【Leecode】两数相加
学习了链表结构,链表中元素的访问,对象指针的初始化与赋值,链表的创建(多个节点链接起来),进位计算的表达. 100ms /** * Definition for singly-linked list. ...
- 【Leecode】两数之和
学习使用标准模板库(STL)中的map,hash_map.涉及数据结构知识:哈希表,红黑树. map的使用方法 https://www.cnblogs.com/fnlingnzb-learner/p/ ...
- 【UVA1194】Machine Schedule
题目大意:给定 N 个任务和两台机器,每个任务可以在任意一台机器上执行,每台机器有 N 个启动状态,不同任务需要机器在不同的状态下执行,求执行所有任务需要多少个不同的状态. 题解:由于一个任务一定要被 ...
- 【贪心】hdu6180 Schedule
题意:给你n个任务的开始时间和结束时间,一个机器同时最多执行一个任务,问你最少要几个机器.保证机器最少的前提下,问你每个机器的开动时间(最后一次关闭-第一次开启)之和最少是多少. 把这些线段画在数轴上 ...
- 【Poj1325】Machine Schedule机器调度
目录 List Description Input Output Sample Input Sample Output HINT Solution Code Position: http://poj. ...
- 【leecode】小练习(简单8题)
def twoSum(nums, target): """ 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[ ...
- 【leecode】独特的电子邮件地址
每封电子邮件都由一个本地名称和一个域名组成,以 @ 符号分隔. 例如,在 alice@leetcode.com中, alice 是本地名称,而 leetcode.com 是域名. 除了小写字母,这些电 ...
随机推荐
- 第06组 Alpha冲刺(3/6)
队名:拾光组 组长博客链接 作业博客链接 团队项目情况 燃尽图(组内共享) 组长:宋奕 过去两天完成了哪些任务 主要完成了用户论坛模块的接口设计 完善后端的信息处理 GitHub签入记录 接下来的计划 ...
- JVM 类加载器ClassLoader源码学习笔记
类加载 在Java代码中,类型的加载.连接与初始化过程都是在程序运行期间完成的. 类型可以是Class,Interface, 枚举等. Java虚拟机与程序的生命周期 在如下几种情况下,Java虚拟机 ...
- nginx基础模块
http://www.nginx.cn/doc/ 基础模块 HTTP Core模块* HTTP Upstream 模块 HTTP Access 模块 HTTP Auth Basic 模块 HTTP A ...
- NAXSI means Nginx Anti XSS & SQL Injection. NAXSI is an open-source, high performance, low rules maintenance WAF for NGINX
nbs-system/naxsi: NAXSI is an open-source, high performance, low rules maintenance WAF for NGINXhttp ...
- Microservices in action: java(spring) and .net
Manning | Homehttps://www.manning.com/ What is a Microservice? | Manninghttps://freecontent.manning. ...
- leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)
914. Flip Game https://www.cnblogs.com/grandyang/p/5224896.html 从前到后遍历,遇到连续两个'+',就将两个加号变成'-'组成新的字符串加 ...
- Python3基础 内置函数 id
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- RabbitMQ 入门教程(PHP版) 延迟队列,延迟任务
延迟任务应用场景 场景一:物联网系统经常会遇到向终端下发命令,如果命令一段时间没有应答,就需要设置成超时. 场景二:订单下单之后30分钟后,如果用户没有付钱,则系统自动取消订单. 场景三:过1分钟给新 ...
- Shenzhen Wanze Technology Co., Ltd.隐私协议
本隐私权政策详细说明了Shenzhen Wanze Technology Co., Ltd.团队(“我们”或“我们的”)通过我们的应用程序和网站收集的信息,以及我们将如何使用这些信息. 1.我们不会通 ...
- django项目mysite
项目建立 建立项目mysite 各文件和目录解释: 外层的mysite/目录与Django无关,只是你项目的容器,可以任意重命名. manage.py:一个命令行工具,用于与Django进行不同方式的 ...