forked from arkingc/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
210.cpp
34 lines (31 loc) · 1010 Bytes
/
210.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
class Solution {
public:
vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<vector<int> > graph(numCourses);
vector<int> indegrees(numCourses,0); //顶点的入度,即有多少边指向该顶点
vector<int> res;
for(auto p : prerequisites){
indegrees[p.first]++;
graph[p.second].push_back(p.first);
}
deque<int> q;
for(int v = 0;v < numCourses;v++)
if(indegrees[v] == 0){
q.push_back(v);
res.push_back(v);
}
while(!q.empty()){
int v = q.front();
q.pop_front();
for(int v2 : graph[v])
if(--indegrees[v2] == 0){
q.push_back(v2);
res.push_back(v2);
}
}
for(int indegree : indegrees)
if(indegree)
return vector<int>();
return res;
}
};