# My code passed all the tests except the last one. Haven't figured out why.

• Is there something wrong with my code or because there are so many cases that the test cannot handle?

``````class Solution {
private:
bool topsort(unordered_set<int>& visited, unordered_set<int>& rec, unordered_map<int,
unordered_set<int>>& graph, vector<int>& order,const int& n) {

if (rec.count(n)) return false;
if (visited.count(n)) return true;

visited.insert(n);
rec.insert(n);

for (auto iter = graph[n].begin(); iter != graph[n].end(); iter ++) {
if (!topsort(visited, rec, graph, order, *iter)) return false;
}

rec.erase(n);

order.push_back(n);
return true;
}

public:
vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<int> order;
vector<int> result;

unordered_map<int, unordered_set<int>> graph;

unordered_set<int> visited;
unordered_set<int> rec;

for (int i = 0; i < prerequisites.size(); i ++) {
int c2 = prerequisites[i].first;
int c1 = prerequisites[i].second;
unordered_set<int> tmp;
if (graph.count(c1) == 0) {
graph[c1] = tmp;
graph[c1].insert(c2);
}
else if (graph[c1].count(c2) == 0) {
graph[c1].insert(c2);
}
}

for (auto iter = graph.begin(); iter != graph.end(); iter ++) {

if (!topsort(visited, rec, graph, order, iter -> first)) return result;
}
reverse(order.begin(), order.end());
for (int i = 0; i < numCourses; i ++) {
if (!visited.count(i)) order.push_back(i);

}

result = order;

return result;

}
``````

};

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.