-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCourse Schedule II.cpp
78 lines (58 loc) · 1.82 KB
/
Course Schedule II.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Solution by Rahul Surana
***********************************************************
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.
For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
Return the ordering of courses you should take to finish all courses.
If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
stack<int> S;
vector<int> V;
vector<int> P;
bool cycle;
map<int, vector<int> > G;
void DFS(int n)
{
for(int i=0;i<n;++i)
{
if(!V[i]) DFS_recur(i);
}
}
void DFS_recur(int curr)
{
if(cycle) return;
V[curr] = true;
P[curr] = true;
for(int i : G[curr])
{
if(P[i] == true) cycle = true;
if(!V[i]) DFS_recur(i);
}
S.push(curr);
P[curr] = false;
}
vector<int> findOrder(int n, vector<vector<int>>& prerequisites)
{
vector<int> Ans;
V.assign(n, false);
P.assign(n, false);
cycle = false;
for(auto i : prerequisites)
{
G[i[1]].push_back(i[0]);
}
DFS(n);
while(!S.empty())
{
Ans.push_back(S.top());
S.pop();
}
if(cycle) return {};
return Ans;
}
};