#include using namespace std; class Solution { public: vector findOrder(int numCourses, vector> &prerequisites) { vector result; vector fake; vector degree(numCourses, 0); unordered_map> map; for (vector prerequisite : prerequisites) { map[prerequisite[1]].push_back(prerequisite[0]); degree[prerequisite[0]]++; } queue q; for (int i = 0; i < numCourses; i++) { if (degree[i] == 0) { q.push(i); } } while (!q.empty()) { int cur = q.front(); result.push_back(cur); q.pop(); for (int next : map[cur]) { degree[next]--; if (degree[next] == 0) q.push(next); } } return result.size() == numCourses ? result : fake; } };