和这道题目一样:https://www.cnblogs.com/dacc123/p/12306344.html
拓扑排序,把排序结果打印出来了。
class Solution {
public:
vector<int> edge[10005];
int a[10005];
int b[10005];
int vis[10005];
vector<int> ans;
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
for(int i=0;i<prerequisites.size();i++)
{
edge[prerequisites[i][1]].push_back(prerequisites[i][0]);
a[prerequisites[i][0]]++;
b[prerequisites[i][1]]++;
}
while(1)
{
int tag=0;
for(int i=0;i<numCourses;i++)
{
if(vis[i]==1)
continue;
if(a[i]!=0)
continue;
for(int j=0;j<edge[i].size();j++)
{
a[edge[i][j]]--;
}
ans.push_back(i);
tag=1;
vis[i]=1;
}
if(tag==0)
break;
}
if(ans.size() != numCourses)
ans.clear();
return ans;
}
};