https://leetcode.com/problems/course-schedule-ii/
class Solution {
public:
vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<int> res;
vector<int> inDgree(numCourses,0);
map<int, vector<int> >adjNode;
int len = prerequisites.size();
for (int i = 0; i < len; i++){
pair<int, int> p = prerequisites[i];
//自环
if (p.first == p.second)
return res;
//平行边
if (find(adjNode[p.second].begin(), adjNode[p.second].end(), p.first) == adjNode[p.second].end()){
adjNode[p.second].push_back(p.first);
inDgree[p.first]++;
}
}
queue<int> Q;
for (int i=0; i < numCourses; i++){
if (inDgree[i] == 0)
Q.push(i);
}
while (!Q.empty()){
int front = Q.front();
Q.pop();
res.push_back(front);
vector<int> adj = adjNode[front];
for (int i:adj){
inDgree[i]--;
if (inDgree[i] == 0){
Q.push(i);
}
}
}
if (res.size() == numCourses){
return res;
}
else{
return vector<int>();
}
}
};