#include using namespace std; class Solution { public: bool canFinish(int numCourses, vector> &prerequisites) { vector> graph(numCourses, vector()); for (auto num : prerequisites) { graph[num[1]].push_back(num[0]); } vector visit(numCourses); for (int i = 0; i < numCourses; i++) { if (!dfs(graph, visit, i)) return false; } return true; } bool dfs(vector> &g, vector &visit, int i) { if (visit[i] == -1) return false; if (visit[i] == 1) return true; visit[i] = -1; for (auto a : g[i]) { if (!dfs(g, visit, a)) return false; } visit[i] = 1; return true; } };