方法一:dfs
class Solution {
public:
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
vector<int> res;
vector<vector<int>> graph(numCourses,vector<int>(0));
vector<int>indegree(numCourses,0);
queue<int> que;
for(auto vec:prerequisites)
{
graph[vec[1]].push_back(vec[0]);
++indegree[vec[0]];
}
for(int i=0;i<numCourses;i++)
{
if(indegree[i]==0)
{
que.push(i);
}
}
while(!que.empty())
{
int second=que.front();
que.pop();
res.push_back(second);
--numCourses;
for(auto first:graph[second])
{
--indegree[first];
if(indegree[first]==0)
{
que.push(first);
}
}
}
if(numCourses!=0)
{
res.clear();
}
return res;
}
};