-
Notifications
You must be signed in to change notification settings - Fork 0
/
Course Schedule.cpp
70 lines (67 loc) · 1.53 KB
/
Course Schedule.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
#include<iostream>
#include<string>
#include<vector>
#include <cctype>
#include<algorithm>
#include<math.h>
#include<unordered_set>
#include<stack>
using namespace std ;
typedef struct ListNode{
int val;
ListNode * next;
} ListNode;
class Solution {
public:
// bool canFinish2(int numCourses, vector<pair<int, int> >& prerequisites) {
// }
bool canFinish(int numCourses, vector<pair<int, int> >& prerequisites) {
int i,j,foo,bar,res;
unordered_set<int> temp;
vector<unordered_set<int> > graph(numCourses,temp);
vector<int> flag(numCourses,0);
for(i=0;i<prerequisites.size();i++){
graph[prerequisites[i].second].insert(prerequisites[i].first);
}
for(i=0;i<numCourses;i++){
stack<int> cur;
for(auto it = graph[i].begin(); it != graph[i].end(); ++it)
cur.push(*it);
while(!cur.empty()){
foo=cur.top();
// cout<<foo<<endl;
cur.pop();
if(flag[foo]==0){
for(auto iter = graph[foo].begin(); iter != graph[foo].end(); ++iter){
bar=*iter;
if(bar==i)
return 0;
if(graph[i].find(bar)==graph[i].end()){
cur.push(bar);
graph[i].insert(bar);
}
}
}
}
flag[i]=1;
}
return 1;
}
void print()
{
vector<pair<int, int> > A;
A.push_back(make_pair(0,1));
A.push_back(make_pair(1,3));
A.push_back(make_pair(3,1));
A.push_back(make_pair(3,2));
cout<<canFinish(4,A)<<endl;
}
};
int main()
{
Solution test;
// cout<<test.Low("A man, a plan, a canal: Panama")<<endl;
test.print();
// cout<<atoi("2147483648")<<endl;
system("pause");
}