<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 {
public:
    typedef unordered_map<int, vector<int>> adj_map_t; // key - source, value - list of destinations

    bool isTree(int node, adj_map_t& adj_map, vector<bool>& visited, vector<bool> path)
    {
        visited[node] = true;
        path[node] = true;
        vector<int>::iterator itr = adj_map[node].begin(), end = adj_map[node].end();
        for(; itr != end; ++itr)
        {
            if(!visited[*itr] && isTree(*itr, adj_map, visited, path))
                return true;
            else if(path[*itr])
                return true;
        }
        path[node] = false;
        return false;
    }

    bool dfs(int node, vector<bool>& visited, adj_map_t& adj_map, stack<int>& st)
    {
        if(visited[node] == true)
            return false;
        visited[node] = true;
        vector<int>::iterator itr = adj_map[node].begin(), end = adj_map[node].end();
        for(; itr != end; ++itr)
        {
            if(!visited[*itr])
                if(!dfs(*itr, visited, adj_map, st))
                    return false;
        }
        st.push(node);
        return true;
    }

    bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
        vector<bool> undep_courses(numCourses, true);
        vector<bool> visited(numCourses, false);
        
        adj_map_t adj_map;
        stack<int> st;

        for(vector<int> edge : prerequisites)
        {
            if(edge[0] == edge[1])
                return false;
            if(adj_map.find(edge[1]) == adj_map.end())
                adj_map[edge[1]] = vector<int>(1, edge[0]);
            else
                adj_map[edge[1]].push_back(edge[0]);
            undep_courses[edge[0]] = false;
        }
        
        for(int i = 0; i < numCourses; ++i)
        {
            if(visited[i] == true)
                continue;
            vector<bool> path(numCourses, false);
            if(isTree(i, adj_map, visited, path))
                return false;
        }
        return true;
    }
};
22500