点击打开链接
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <string.h>
#include <algorithm>
#include <vector>
#include <numeric>
#include <limits>
#include <math.h>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
class Solution {
public:
vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
int Arr[numCourses+5][numCourses+5];
memset(Arr, -1, sizeof(Arr));
int d[numCourses+5];
memset(d, 0, sizeof(d));
int len = prerequisites.size();
for(int i=0; i<len;i++){
Arr[prerequisites[i].second][prerequisites[i].first] = 1;
}
for(int i=0;i<numCourses;i++){
for(int j=0;j<numCourses;j++){
if(Arr[i][j]==1){
d[j]+=1;
}
}
}
queue<int> Q;
for(int i=0;i<numCourses;i++){
if(d[i]==0) Q.push(i);
}
int u=0;
vector<int> result;
while (!Q.empty()) {
u= Q.front(); Q.pop();
result.push_back(u);
for(int j=0; j<numCourses; j++){
if(Arr[u][j]==1){
d[j]-=1;
if(d[j]==0){
Q.push(j);
Arr[u][j]=-1;
}
}
}
}
if(result.size()!=numCourses){
result.clear();
return result;
}
return result;
}
};
int main()
{
Solution temple;
return 0;
}