当前位置:网站首页>210. Schedule II - depth traversal
210. Schedule II - depth traversal
2022-06-30 01:41:00 【The_ Dan】
class Solution {
public:
// It's the curriculum 207 An upgraded version of the question , The difference is that the results need to be output in an array
// Different places are marked , For those who don't understand the idea, it is suggested to turn to page 207 Notes to questions
vector<vector<int>> vecVecInt;
vector<int> visited;
bool flag = true;
vector<int> ans;
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
visited.resize(numCourses);
vecVecInt.resize(numCourses);
for(auto i : prerequisites)
vecVecInt[i[0]].push_back(i[1]);
for(int i = 0; i < numCourses && flag; ++i){
if(!visited[i]) // You have to add , Otherwise, the output will be repeated , And can improve efficiency
dfs(i);
}
if(flag) // If you can finish all the courses , Output the answer
return ans;
else // Otherwise, return an empty array
return {
};
}
void dfs(int n){
visited[n] = 1;
int size = vecVecInt[n].size();
for(int i = 0; i < size; ++i){
if(flag && visited[vecVecInt[n][i]] == 0){
dfs(vecVecInt[n][i]);
if(!flag)
return;
}
else if(visited[vecVecInt[n][i]] == 1){
flag = false;
return;
}
}
visited[n] = 2;
ans.push_back(n); // Depth traversal , So just put it at the end
}
};
Accepted
44/44 cases passed (24 ms)
Your runtime beats 31.18 % of cpp submissions
Your memory usage beats 20.07 % of cpp submissions (14.2 MB)
边栏推荐
- Cookie encryption 10
- How to deal with occasional bugs?
- Varnish 基础概览1
- Pytorch 修改hook源码 获取Per-Layer输出参数(带layer name)
- Comprendre le principe AQS (organigramme et schéma de file d'attente synchrone)
- Unity2d-- add keys to animation and bind events
- Cookie encryption 8
- Varnish 基础概览2
- 搞透AQS原理(流程圖及同步隊列圖解)
- DTW学习(dynamic time warping)——思想、代码实现
猜你喜欢

Understand AQS principle (flow chart and synchronous queue diagram)
![【图神经网络】图分类学习研究综述[3]:图分类方法评价及未来研究方向](/img/b1/2afa73a14b2f41b7a65c4c2d261e6a.png)
【图神经网络】图分类学习研究综述[3]:图分类方法评价及未来研究方向

C语言 成绩排名
![[recommendation system] concise principle and code implementation of user based collaborative filtering](/img/3b/d46e37e7ae245cf2931d32d1ebbe4c.png)
[recommendation system] concise principle and code implementation of user based collaborative filtering

想转行,但不知道自己要做什么工作比较好?

ctfshow 大赛原题 680-695

JS reverse request parameter encryption:

Sentinel source code analysis Part 7 - sentinel adapter module - Summary

DTW学习(dynamic time warping)——思想、代码实现

Circular right shift of array elements in C language
随机推荐
MySQL monitoring 2
魔百盒CM201-2-CH-Hi3798MV300-300H-EMMC和NAND_红外蓝牙语音_通刷固件包
Varnish foundation overview 6
Varnish 基础概览8
MySQL monitoring 5
Mysql 监控5
当大学毕业感到迷茫怎么办?
(1)基础学习——图解pin、pad、port、IO、net 的区别
Mysql 监控1
Sentinel source code analysis Part 7 - sentinel adapter module - Summary
【二叉树】最大二叉树 II
Varnish 基础概览5
cookie加密9
AI landing manufacturing: intelligent robots should have these four abilities
Error reporting in Luban H5 installation
Is the processor the main factor in buying a mobile phone?
Geotools wkt coordinate system conversion
Mobaihe cm201-2-ch-hi3798mv300-300h-emmc and NAND_ Infrared Bluetooth voice_ Brush firmware package
Varnish 基础概览4
Kubernetes core object overview details