当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
分布式服务治理
光弘科技:公司在印度为小米、三星、OPPO、诺基亚提供智能手机等产品的制造服务
api的封装
Share these new Blender plugins that designers must not miss in 2022
瑞吉外卖学习笔记3
程序发生run time error原因及解决方案
干货:从零设计高并发架构
带你玩转“超大杯”ECS特性及实验踩坑【华为云至简致远】
Take you to play with the "Super Cup" ECS features and experiment on the pit [HUAWEI CLOUD is simple and far]
【云原生】云原生相关技术概念总结
随机推荐
光弘科技:公司在印度为小米、三星、OPPO、诺基亚提供智能手机等产品的制造服务
NFT质押挖矿分红系统开发逻辑功能介绍
Smobiler的复杂控件的由来与创造
我分析30w条数据后发现,西安新房公摊最低的竟是这里?
线程本地存储 ThreadLocal
bzoj3693 round table hall theorem + segment tree
如何制作网页
找工作的我看了国聘app
目前安全靠谱的国内期货开户流程?
Dry goods: design high concurrency architecture from scratch
hdu2475 Box
MySQL数据库的简介及select语句的执行流程
小实验:实现一个用于计算(包括加减乘除)的小程序
sql合并连续时间段内,某字段相同的行。
firewall高级配置
[Unity Starter Plan] Making RubyAdventure02 - Handling Tile Maps & Collision
bzoj3262 Flowers bloom on Mo
全志V853芯片Tina下RTSP环境搭建方法
2022年中国全民健身发展白皮书
带你玩转“超大杯”ECS特性及实验踩坑【华为云至简致远】