-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path207.cpp
More file actions
37 lines (37 loc) · 1.06 KB
/
207.cpp
File metadata and controls
37 lines (37 loc) · 1.06 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
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<int> indegree(numCourses,0);
unordered_map<int,unordered_set<int>> graph;
for(auto& p: prerequisites){
int u = p[1];
int v = p[0];
graph[u].insert(v);
indegree[v]++;
}
queue<int> q;
for(int i = 0 ;i<numCourses; i++)
if(indegree[i]==0){
q.push(i);
indegree[i]--;
}
while(!q.empty()){
auto u = q.front();q.pop();
if(graph.count(u)){
for(auto& v: graph[u]){
if(indegree[v]>0){
indegree[v]--;
if(indegree[v] == 0){
q.push(v);
indegree[v]--;
}
}
}
}
}
for(int i=0; i<numCourses; i++)
if(indegree[i]>=0)
return false;
return true;
}
};