当前位置:网站首页>207. curriculum - graph theory, depth traversal
207. curriculum - graph theory, depth traversal
2022-06-30 01:40:00 【The_ Dan】
class Solution {
public:
bool flag = true;
vector<int> visited;
vector<vector<int>> vecVecInt;
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
// It is actually a graph theory problem , See if there is a ring
// So we just look at each course as a point , Prerequisite course relationship as a line , Then observe whether there is a ring
visited.resize(numCourses); // structure numCourses The size of visited Array , Record whether the point has been visited
vecVecInt.resize(numCourses); // structure numCourses The size of vecVecInt Array , Record the connection between this point and other points
for(auto i : prerequisites){
// Traverse all prerequisite relationships , Structure
vecVecInt[i[0]].push_back(i[1]);
}
for(auto i = 0; i < numCourses && flag; ++i){
// Traverse each point , Observe whether there is a ring from this point
dfs(i);
}
return flag; //flag Used to record whether a ring exists in the global , The default is true, Existence as false
}
void dfs(int n){
// Depth traversal graph
if(visited[n] == 2) //2 Indicates that the point has been visited , And not the point on the ring
return;
visited[n] = 1; // Mark as the point visited
for(auto i : vecVecInt[n]){
if(visited[i] == 0){
//visited == 0 I haven't visited
dfs(i); // Access this point
if(!flag) // If after the visit ,flag Turn into false, It indicates that the ring relationship is found during the access
return; // Quickly end all access
}
else if(visited[i] == 1){
//visited == 1 Explain that you have visited before , This is the second visit , That is, there is a ring relation
flag = false; // Discover ring relationships ,flag Set as false
return;
}
}
visited[n] = 2; // This point is a safe point
}
};
Accepted
51/51 cases passed (24 ms)
Your runtime beats 33.99 % of cpp submissions
Your memory usage beats 23.46 % of cpp submissions (14.1 MB)
边栏推荐
- js Array.from()的5个便捷应用
- TP-LINK configure WiFi authentication method for wireless Internet SMS
- Varnish 基础概览8
- The (3n+1) conjecture that C language kills people without paying for their lives
- [mrctf2020]ezpop-1 | PHP serialization
- OpenCV和Image之间的转换(亲测有效)
- Cub school learning: manual query and ADC in-depth use
- Pytorch中transforms的用法整理
- Write this number in C
- Pytorch模型训练到哪里找预训练模型?
猜你喜欢
![[graph neural network] summary of graph classification study [3]: evaluation of graph classification methods and future research directions](/img/b1/2afa73a14b2f41b7a65c4c2d261e6a.png)
[graph neural network] summary of graph classification study [3]: evaluation of graph classification methods and future research directions

【推荐系统】基于用户的协同过滤简明原理与代码实现

Derivation of univariate polynomial in C language

Seata 与三大平台携手编程之夏,百万奖金等你来拿

C语言 害死人不偿命的(3n+1)猜想

手势数字启蒙学习机

Cookie encryption 13

JS content confusion, return content encryption

【机器学习Q&A】数据抽样和模型验证方法、超参数调优以及过拟合和欠拟合问题

cookie加密8
随机推荐
ES6 one line code for array de duplication
Cookie加密10
Interface Association of postman
(1)基础学习——图解pin、pad、port、IO、net 的区别
Geotools: common tools for mutual conversion of wkt, geojason, feature and featurecollection
当大学毕业感到迷茫怎么办?
Mysql 监控6
[pytorch actual combat] generate confrontation network Gan: generate cartoon character avatars
C语言 换个格式输出整数
Database application
C语言 继续(3n+1)猜想
【二叉树】最大二叉树 II
Cookie加密15 登录加密
Varnish 基础概览4
Application features and functions of painting Aquarium
Unity2D--给动画添加关键帧并绑定事件
OpenCV和Image之间的转换(亲测有效)
手势数字启蒙学习机
【图神经网络】图分类学习研究综述[3]:图分类方法评价及未来研究方向
Cookie encryption 9