-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode_207.cpp
40 lines (36 loc) · 936 Bytes
/
leetcode_207.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
class Solution {
public:
vector<vector<int>> edges;
vector<int> inedg;
vector<int> result;
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
edges.resize(numCourses);
inedg.resize(numCourses);
for(auto info: prerequisites){
edges[info[1]].push_back(info[0]);
inedg[info[0]]++;
}
queue<int> q;
for (int i = 0; i < numCourses; i++){
if(inedg[i] == 0){
q.push(i);
}
}
while(!q.empty()){
int u = q.front();
q.pop();
result.push_back(u);
for (auto v: edges[u]){
--inedg[v];
if(inedg[v] == 0){
q.push(v);
}
}
}
if (result.size() != numCourses){
return false;
} else {
return true;
}
}
};