-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkahn's algorithm.cpp
More file actions
40 lines (40 loc) · 1.18 KB
/
Copy pathkahn's algorithm.cpp
File metadata and controls
40 lines (40 loc) · 1.18 KB
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
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
unordered_set<int> result;
unordered_set<int> next;
int a[numCourses]={0};
bool b[numCourses][numCourses];
memset(b,0,sizeof(b));
int pl=prerequisites.size();
for (int i=0;i<pl;i++)
{
a[prerequisites[i].first]++;
b[prerequisites[i].first][prerequisites[i].second]=1;
}
for (int i=0;i<numCourses;i++)
{
if(a[i]==0)
next.insert(i);
}
unordered_set<int>::iterator it;
while (!next.empty())
{
it=next.begin();
int nn=(*it);
result.insert(nn);
next.erase(it);
for (int i=0;i<numCourses;i++)
if(b[i][nn])
{
b[i][nn]=false;
pl--;
a[i]--;
if(a[i]==0) next.insert(i);
}
}
if(pl==0) return true;
else return false;
}
};
//Next challenges: Alien Dictionary// Minimum Height Trees// Sequence Reconstruction// Course Schedule III