```
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<int> G[numCourses];
int de[numCourses]{};
queue<int> Q;
for (auto rel : prerequisites)
{
G[rel.second].push_back(rel.first);
de[rel.first] ++;
}
for (int i = 0; i < numCourses; ++ i)
if (de[i] == 0)
Q.push(i);
int cnt(numCourses);
while (!Q.empty())
{
int cur = Q.front();
Q.pop();
cnt --;
for (int u : G[cur])
{
de[u] --;
if (de[u] == 0)
Q.push(u);
}
}
return cnt == 0;
}
```

We only need to momorize Topological sort is meant to sort nodes in a directed acyclic graph.