-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourseSchedule.cpp
More file actions
37 lines (35 loc) · 901 Bytes
/
CourseSchedule.cpp
File metadata and controls
37 lines (35 loc) · 901 Bytes
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:
void fastIO(){
std::ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
bool canFinish(int n, vector<vector<int>>& pre) {
fastIO();
vector<vector<int>> graph(n, vector<int>(0));
vector<int> indegree(n, 0);
for(auto x : pre){
graph[x[0]].push_back(x[1]);
indegree[x[1]]++;
}
queue<int> q;
for(int i = 0; i<n; i++){
if(indegree[i] == 0){
q.push(i);
}
}
vector<int> res;
while(!q.empty()){
auto t = q.front();
q.pop();
res.push_back(t);
for(auto next : graph[t]){
indegree[next]--;
if(indegree[next] == 0){
q.push(next);
}
}
}
return res.size() == n;
}
};