forked from super30admin/BFS-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourse_scheudle_207.cpp
More file actions
48 lines (45 loc) · 1.21 KB
/
Copy pathcourse_scheudle_207.cpp
File metadata and controls
48 lines (45 loc) · 1.21 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
41
42
43
44
45
46
47
48
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
queue<int> q;
int count=0;
unordered_map<int, vector<int>> map;
vector<int> inorder(numCourses, 0);
for(int i=0;i<prerequisites.size();i++)
{
inorder[prerequisites[i][0]]++;
map[prerequisites[i][1]].push_back(prerequisites[i][0]);
}
for(int i=0;i<inorder.size();i++)
{
if(inorder[i]==0)
{
q.push(i);
count++;
}
}
if(q.empty())
return false;
while(!q.empty())
{
int node=q.front();
q.pop();
for(auto [key,val]: map)
{
if(key==node)
{
for(auto x: val)
{
inorder[x]--;
if(inorder[x]==0)
{
q.push(x);
count++;
}
}
}
}
}
return count==numCourses;
}
};