```
class Solution:
# @param {integer} numCourses
# @param {integer[][]} prerequisites
# @return {boolean}
def canFinish(self, numCourses, prerequisites):
myset = [-1 for _ in xrange(numCourses)]
for p,s in sorted(prerequisites):
myset[s] = p
while p != -1:
p = myset[p]
if p == s:
return False
return True
```