当前位置:网站首页>leetcode207.课程表(判断有向图是否有环)
leetcode207.课程表(判断有向图是否有环)
2022-07-30 12:26:00 【深海里的鱼(・ω<)*】
方法一:拓扑排序
class Solution {
public:
vector<vector<int>> get_adj_list(int numCourses,vector<vector<int>> &prerequisites){
//获得图的邻接表
vector<vector<int>> graph(numCourses);
for(auto &e:prerequisites){
graph[e[0]].push_back(e[1]);
}
return graph;
}
bool topo_sort(vector<vector<int>> &graph){
//拓扑排序判断是否有环
int cnt=0;//拓扑序列中的顶点个数
int num_v=graph.size();//图中顶点个数
vector<int> deg(num_v);//各个顶点的度
stack<int> st;//存入度为0的顶点
//计算各个顶点的度
for(int i=0;i<num_v;i++){
for(auto &v:graph[i]){
deg[v]++;
}
}
//将入度为0的顶点入栈
for(int i=0;i<deg.size();i++){
if(deg[i]==0){
st.push(i);
}
}
while(!st.empty()){
int v=st.top(); //顶点v
st.pop();
cnt++;
for(auto &i:graph[v]){
//去掉顶点v看看是否会产生度为0的顶点
deg[i]--;
if(deg[i]==0){
st.push(i);
}
}
}
if(cnt<num_v){
return false;
}
else{
return true;
}
}
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
auto adj_list=get_adj_list(numCourses,prerequisites);
return topo_sort(adj_list);
}
};
方法二:dfs深搜
class Solution {
public:
vector<vector<int>> get_adj_list(int numCourses,vector<vector<int>> &prerequisites){
//获得图的邻接表
vector<vector<int>> graph(numCourses);
for(auto &e:prerequisites){
graph[e[0]].push_back(e[1]);
}
return graph;
}
void dfs(vector<vector<int>> &graph,int u,vector<int> &vis,vector<int> &in_dfs,bool &loop){
if(loop){
//检测到环
return ;
}
in_dfs[u]=1;
vis[u]=1;
for(auto &v:graph[u]){
if(in_dfs[v]){
//如果这个顶点在本次dfs中
loop=true;
return ;
}
if(!vis[v]){
//如果这个顶点还未访问过
dfs(graph,v,vis,in_dfs,loop);
}
}
in_dfs[u]=0;
}
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
auto graph=get_adj_list(numCourses,prerequisites);
vector<int> vis(numCourses);
vector<int> in_dfs(numCourses);
bool loop=false;
for(int i=0;i<numCourses;i++){
if(!vis[i]){
dfs(graph,i,vis,in_dfs,loop);
if(loop){
break;
}
}
}
return !loop;
}
};
边栏推荐
- What happened when the computer crashed?
- Anaconda\Scripts\pip-script.py is not present ? 解决方案
- Lake storehouse which electricity (2) of the project: project using technology and version and the environment
- 来n遍剑指--04. 二维数组中的查找
- int a=8,a=a++,a? int b=8,b=b+1,b?
- [PostgreSQL] - explain SQL分析介绍
- [ASP.NET Core] Dependency Injection for Option Classes
- 手撕读写锁性能测试
- 数据湖(十八):Flink与Iceberg整合SQL API操作
- 企业如何成功完成云迁移?
猜你喜欢
Decoding Redis' most overlooked high CPU and memory usage issues
13-GuliMall Basics Summary
展厅全息投影所具备的三大应用特点
Rust 从入门到精通02-安装
Unity Beginner 6 - Simple UI production (blood bar production) and audio addition and NPC dialogue bubbles (2d)
物理服务器与虚拟机:主要区别和相似之处
监控界的最强王者,没有之一!
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
【记一个kaggle划水比赛】PetFinder.my - Pawpularity Contest 宠物预测
打破原则引入SQL,MongoDB到底想要干啥???
随机推荐
no matching host key type found. Their offer: ssh-rsa
Breaking the principle and introducing SQL, what does MongoDB want to do???
手撕读写锁性能测试
What happened when the computer crashed?
EXCEL解决问题:如何查找目标区域,是否包含指定字符串?
Js - 内置对象
[PostgreSQL] - 存储结构及缓存shared_buffers
从“校园贷”到“直播带货”,追风少年罗敏一直行走在风口浪尖
Mysql索引结构
JD.com was brutally killed by middleware on two sides. After 30 days of learning this middleware booklet, it advanced to Ali.
历时两月,终拿字节跳动offer,算法面试题分享「带答案」
Dry Goods Sharing: Various Implementation Methods of Bean Management Factory with Great Use of Small Skills
概率论的学习整理1: 集合和事件
Xms Xmx PermSize MaxPermSize 区别
大手笔!两所“双一流”大学,获75亿元重点支持!
ES6 Set与Map是什么,如何使用
展厅全息投影所具备的三大应用特点
手慢无!阿里亿级流量高并发系统设计核心原理全彩笔记现实开源
奇异值分解(SVD)原理与在降维中的应用(附带例题讲解)(纯理论)
Zhou Hongyi: Microsoft copied the 360 security model and became the largest security company in the United States