当前位置:网站首页>力扣207,课程表
力扣207,课程表
2022-08-08 15:58:00 【瀛台夜雪】
力扣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] 中的所有课程对 互不相同
解法一,使用广度优先搜素,借用队列
//判断是否是死循环
//判断是否存在环
//将课程看作是有向边
//使用队列进行辅助BFS
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
//维持度和边的两张表
vector<vector<int>>edges(numCourses,vector<int>());
vector<int>indeg(numCourses);
//填入数据到度表中以及边表中
for(auto info:prerequisites)
{
edges[info[1]].push_back(info[0]);
indeg[info[0]]++;
}
//使用队列来尽心辅助,存储入度为0的点
queue<int>que;
for(int i=0;i<numCourses;i++)
{
if(indeg[i]==0)
{
que.push(i);
}
}
//设置标志位,用与检测队列中出现的数目是否与待修课程数相同
int visited=0;
while(!que.empty())
{
++visited;
int temp=que.front();
que.pop();
//将与该出队列的顶点有关的边,入度减一
//入度为0的,继续入栈,这里可以规避环
for(auto v:edges[temp])
{
indeg[v]--;
if(indeg[v]==0)
{
que.push(v);
}
}
}
return visited==numCourses;
}
解法二,使用深度优先搜索
//保存有三种状态,未搜索,搜索中,已完成
bool canFinish2(int numCourses, vector<vector<int>>& prerequisites) {
//初始化参量
vector<vector<int>>edges(numCourses,vector<int>());
vector<int>visited(numCourses);
bool valid=true;
//读取数组,将数据导入进边表
for(auto info:prerequisites)
{
edges[info[1]].push_back(info[0]);
}
//遍历每一个未被搜索的结点
for(int i=0;i<numCourses&&valid;i++)
{
//当结点是未被搜索的,则调用dfs
if(!visited[i])
{
dfs(i,visited,edges,valid);
}
}
return valid;
}
void dfs(int node,vector<int>&visited,vector<vector<int>>&edges,bool &valid)
{
//设置当前的结点为正在搜索中
visited[node]=1;
//递归调用与当前结点有关的值
for(int v:edges[node])
{
//0 表示存在环
if(visited[v]==0)
{
dfs(v,visited,edges,valid);
if(!valid)
{
return;
}
}
//1 表明存在环
else if(visited[v]==1)
{
valid=false;
return;
}
}
//2 表示已经完成全部结点
visited[node]=2;
}
边栏推荐
- 腾讯云产品可观测最佳实践 (Function)
- Is the current safe and reliable domestic futures account opening process?
- 返回分页查询分类并统计多对多关系表中各分类下的应用数量
- bzoj3262 Flowers bloom on Mo
- Mysql数据库入门学习笔记
- 线程本地存储 ThreadLocal
- First online!Messaging middleware fairy notes, covering the essence of Alibaba's ten years of technology
- 最新30系显卡搭建paddle飞浆环境|含CUDA下载安装
- Streamsets Data Collector 3.12
- sqoop连接MySQL跟本机不一致是为什么
猜你喜欢
随机推荐
leetcode/删除链表的倒数第n个结点
线程本地存储 ThreadLocal
Notes on the development of kindergarten enrollment registration system based on WeChat applet
彻底理解 volatile 关键字及应用场景,面试必问,小白都能看懂!
大佬们,这个测试demo只能获取到全量数据,不能获取增量,我的mysql 已经开启了row模式的bi
hdu2475 Box
Node简介
【服务器数据恢复】Ext4文件系统fsck后mount不上并报错的数据修复案例
如何选择ui设计机构
sql合并连续时间段内,某字段相同的行。
Introduction to Power BI
Guanghong Technology: The company provides manufacturing services for Xiaomi, Samsung, OPPO, Nokia and other products in India
如何制作网页
国泰君安证券新手开户、有安全保障吗?
Synergistic authors open source throttling, 2022 trend of technology foresight (asynchronous programming/container technology)
Teach you how to use uniapp to access chat and IM instant messaging - source code sharing
NFT质押挖矿分红系统开发逻辑功能介绍
目前安全靠谱的国内期货开户流程?
bzoj3262 Flowers bloom on Mo
leetcode/delete the nth node from the bottom of the linked list