-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path210.cpp
More file actions
45 lines (44 loc) · 971 Bytes
/
210.cpp
File metadata and controls
45 lines (44 loc) · 971 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
38
39
40
41
42
43
44
45
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
using namespace std;
class Solution {
public:
vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<int> res;
vector<vector<int> > graph(numCourses,vector<int>(0));
vector<int> in(numCourses,0);
for(auto it = prerequisites.begin(); it != prerequisites.end();it++) {
int from = (*it).second;
int to = (*it).first;
graph[from].push_back(to);
++in[to];
}
queue<int> q;
for(int i = 0; i < numCourses; i++) {
if(in[i] == 0) q.push(i);
}
while(!q.empty()) {
int t = q.front();
res.push_back(t);
q.pop();
for(int i = 0; i < graph[t].size(); i++) {
int next = graph[t][i];
--in[next];
if(in[next] == 0)
q.push(next);
}
}
if(res.size() != numCourses)
res.clear();
return res;
}
};