当前位置:网站首页>Lecture 207, Class Schedule
Lecture 207, Class Schedule
2022-08-08 16:04:00 【Ying night snow】
力扣207,课程表
题目描述
你这个学期必须选修 numCourses 门课程,记为 0 到 numCourses - 1 .
在选修某些课程之前需要一些先修课程. 先修课程按数组 prerequisites 给出,其中 prerequisites[i] = [ai, bi] ,表示如果要学习课程 ai 则 必须 先学习课程 bi .
例如,先修课程对 [0, 1] 表示:想要学习课程 0 ,你需要先完成课程 1 .
请你判断是否可能完成所有课程的学习?如果可以,返回 true ;否则,返回 false .
输入输出样例
输入:numCourses = 2, prerequisites = [[1,0]]
输出:true
解释:总共有 2 门课程.学习课程 1 之前,你需要完成课程 0 .这是可能的.
输入:numCourses = 2, prerequisites = [[1,0],[0,1]]
输出:false
解释:总共有 2 门课程.学习课程 1 之前,你需要先完成课程 0 ;并且学习课程 0 之前,你还应先完成课程 1 .这是不可能的.
tips
1 <= numCourses <= 105
0 <= prerequisites.length <= 5000
prerequisites[i].length == 2
0 <= ai, bi < numCourses
prerequisites[i] 中的所有课程对 互不相同
解法一,Use breadth-first search,借用队列
//Determine whether it is an infinite loop
//判断是否存在环
//Think of classes as directed edges
//Use queues to assistBFS
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
//Two tables of degrees and edges are maintained
vector<vector<int>>edges(numCourses,vector<int>());
vector<int>indeg(numCourses);
//Fill data into the degree table and the edge table
for(auto info:prerequisites)
{
edges[info[1]].push_back(info[0]);
indeg[info[0]]++;
}
//Use queues to help,存储入度为0的点
queue<int>que;
for(int i=0;i<numCourses;i++)
{
if(indeg[i]==0)
{
que.push(i);
}
}
//设置标志位,Use and to check if the number of occurrences in the queue is the same as the number of courses to be taken
int visited=0;
while(!que.empty())
{
++visited;
int temp=que.front();
que.pop();
//The edge that will be associated with this dequeued vertex,入度减一
//入度为0的,继续入栈,The loop can be circumvented here
for(auto v:edges[temp])
{
indeg[v]--;
if(indeg[v]==0)
{
que.push(v);
}
}
}
return visited==numCourses;
}
解法二,使用深度优先搜索
//There are three states for saving,未搜索,搜索中,已完成
bool canFinish2(int numCourses, vector<vector<int>>& prerequisites) {
//initialization parameters
vector<vector<int>>edges(numCourses,vector<int>());
vector<int>visited(numCourses);
bool valid=true;
//读取数组,Import data into the edge table
for(auto info:prerequisites)
{
edges[info[1]].push_back(info[0]);
}
//Traverse each unsearched node
for(int i=0;i<numCourses&&valid;i++)
{
//When the node is not searched,则调用dfs
if(!visited[i])
{
dfs(i,visited,edges,valid);
}
}
return valid;
}
void dfs(int node,vector<int>&visited,vector<vector<int>>&edges,bool &valid)
{
//Set the current node as being searched
visited[node]=1;
//Recursively calls the value associated with the current node
for(int v:edges[node])
{
//0 表示存在环
if(visited[v]==0)
{
dfs(v,visited,edges,valid);
if(!valid)
{
return;
}
}
//1 show that there is a ring
else if(visited[v]==1)
{
valid=false;
return;
}
}
//2 Indicates that all nodes have been completed
visited[node]=2;
}
边栏推荐
猜你喜欢
Smobiler的复杂控件的由来与创造
leetcode--541. 反转字符串II
携手数字创新 共筑国产生态 7月份AntDB与5款产品完成互认证
消除游戏中宝石下落的原理和实现
腾讯又一长达 8 年的服务下架。。。
Guanghong Technology: The company provides manufacturing services for Xiaomi, Samsung, OPPO, Nokia and other products in India
文档管理系统:攻克这3个痛点,解决80%企业文档管理难题
[Unity Starter Plan] Making RubyAdventure02 - Handling Tile Maps & Collision
Streamsets Data Collector 3.12
pytorch安装过程中出现torch.cuda.isavailable()=False问题
随机推荐
‘xxxx‘ is declared but its value is never read.Vetur(6133)
[Unity entry plan] Use the double blood bar method to control the blood loss speed of the damage area
携手数字创新 共筑国产生态 7月份AntDB与5款产品完成互认证
使用 ansible-bender 构建容器镜像
json根据条件存入数据库
C. Build Permutation(构造/数论)
All volunteers V853 chip Tina RTSP environment set up
【Unity入门计划】用双血条方法控制伤害区域减血速度
【愚公系列】华为云云数据库MySQL的体验流程|【华为云至简致远】
Go 语言 Strconv 库常用方法
Share these new Blender plugins that designers must not miss in 2022
bzoj2816 [ZJOI2012] Network
Kubernetes二进制部署高可用集群
手机注册股票开户的流程?网上开户安全?
全网首发!消息中间件神仙笔记,涵盖阿里十年技术精髓
bzoj3262 陌上花开
OpenAI怎么写作「谷歌小发猫写作」
找工作的我看了国聘app
bzoj3693 round table hall theorem + segment tree
SAP系统为什么要迁移上云?