<p>There are a total of <code>numCourses</code> courses you have to take, labeled from <code>0</code> to <code>numCourses - 1</code>. You are given an array <code>prerequisites</code> where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you <strong>must</strong> take course <code>b<sub>i</sub></code> first if you want to take course <code>a<sub>i</sub></code>.</p>

<ul>
	<li>For example, the pair <code>[0, 1]</code>, indicates that to take course <code>0</code> you have to first take course <code>1</code>.</li>
</ul>

<p>Return <code>true</code> if you can finish all courses. Otherwise, return <code>false</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> numCourses = 2, prerequisites = [[1,0]]
<strong>Output:</strong> true
<strong>Explanation:</strong> There are a total of 2 courses to take. 
To take course 1 you should have finished course 0. So it is possible.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> numCourses = 2, prerequisites = [[1,0],[0,1]]
<strong>Output:</strong> false
<strong>Explanation:</strong> There are a total of 2 courses to take. 
To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= numCourses &lt;= 2000</code></li>
	<li><code>0 &lt;= prerequisites.length &lt;= 5000</code></li>
	<li><code>prerequisites[i].length == 2</code></li>
	<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; numCourses</code></li>
	<li>All the pairs prerequisites[i] are <strong>unique</strong>.</li>
</ul>

class Solution:
    def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
        graph = defaultdict(list)
        for i, j in prerequisites:
            graph[i].append(j)
        print(graph)

        visited = set()
        cycle = set()
        def dfs(node):
            if node in visited:
                return True
                
            cycle.add(node)
            for nei in graph[node]:
                if nei in cycle:
                    return False
                if not dfs(nei):
                    return False
            cycle.remove(node)
            visited.add(node)
            return True

        for i in range(numCourses):
            if not dfs(i):
                return False
        return True





    # Topological Sort: DFS
    def temp():
        graph = defaultdict(list)
        for i, j in prerequisites:
            graph[i].append(j)

        visited = set() # 已走過就不用再走一次
        cycle = set()   # 檢查是否有 cycle
        def dfs(i):
            if node in cycle:
                return False
            if node in visited:
                return True
            cycle.add(node)
            for nei in graph[node]:
                if not dfs(nei):
                    return False
            cycle.remove(node)
            visited.add(node)
            return True
        for i in range(numCourses):
            if not dfs(i):
                return False
        return True

    # Neetcode w/ emptying dict
    def temp():
        # Note: use a adjacency list
        dictD = defaultdict(list)
        for a, b in prerequisites:
            dictD[a].append(b)

        visited = set()
        def dfs(i):
            # cyclic
            if i in visited:
                return False
            # if no prerequisite
            if dictD[i] == []:
                return True
            
            # visited 用來看向下探詢的過程中有沒有 cycle，所以最後 visited 是空的也沒關係
            visited.add(i)
            for nei in dictD[i]:
                if not dfs(nei):
                    return False
            visited.remove(i)
            # 已經確定過可以走完，就可以清空條件，加速後續訪問
            dictD[i] = []
            return True

        for i in range(numCourses):
            if not dfs(i):
                return False
        return True
18800