当前位置:网站首页>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)
边栏推荐
- JS anti shake and throttling
- Varnish foundation overview 1
- OpenCV和Image之间的转换(亲测有效)
- C语言 我要通过
- js逆向请求参数加密:
- Mysql 监控2
- (1)基础学习——图解pin、pad、port、IO、net 的区别
- Internal class usage scenarios, syntax and principle explanations
- GeoTools:WKT、GeoJson、Feature、FeatureCollection相互转换常用工具
- C language output integer in another format
猜你喜欢

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

Write this number in C

C语言 说反话

C语言 一元多项式求导

C语言 素数对猜想

Conjecture of prime pairs in C language

TP-LINK configure WiFi authentication method for wireless Internet SMS

Mobaihe cm201-2-ch-hi3798mv300-300h-emmc and NAND_ Infrared Bluetooth voice_ Brush firmware package

The first technology podcast month will be launched soon

JS returned content is encoded by Unicode
随机推荐
Unity2d-- add keys to animation and bind events
How to deal with occasional bugs?
Interview summary
C语言 说反话
TP-LINK configure WiFi authentication method for wireless Internet SMS
Seata 与三大平台携手编程之夏,百万奖金等你来拿
Pytorch模型训练到哪里找预训练模型?
C语言 成绩排名
JS prototype and prototype chain (Lantern Festival meal)
Varnish foundation overview 5
MySQL monitoring 1
[recommendation system] concise principle and code implementation of user based collaborative filtering
What is idempotency? Detailed explanation of four interface idempotence schemes!
Statsmodels notes STL
Ansible ad-hoc 临时命令
[graph neural network] summary of graph classification study [3]: evaluation of graph classification methods and future research directions
Varnish 基础概览8
Spark 离线开发框架设计与实现
What should be paid attention to in the design and production of the Urban Planning Museum
Varnish foundation overview 4