<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 <em>the ordering of courses you should take to finish all courses</em>. If there are many valid answers, return <strong>any</strong> of them. If it is impossible to finish all courses, return <strong>an empty array</strong>.</p>

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

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

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

<pre>
<strong>Input:</strong> numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
<strong>Output:</strong> [0,2,1,3]
<strong>Explanation:</strong> There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.
So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].
</pre>

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

<pre>
<strong>Input:</strong> numCourses = 1, prerequisites = []
<strong>Output:</strong> [0]
</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;= numCourses * (numCourses - 1)</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><code>a<sub>i</sub> != b<sub>i</sub></code></li>
	<li>All the pairs <code>[a<sub>i</sub>, b<sub>i</sub>]</code> are <strong>distinct</strong>.</li>
</ul>

class Solution {
public:
    vector<int> findOrder(int n, vector<vector<int>>& pr) {
        vector<vector<int>> prereqs(n);
        // courses with no prereqs
        vector<vector<int>> adj(n);
        vector<int> courses;
        for(int i=0; i<pr.size(); i++){
            int a = pr[i][0];
            int b = pr[i][1];
            // must do b to pick up a
            adj[b].push_back(a);
            prereqs[a].push_back(b);
        }
        vector<int> visited(n, 0);
        for(int i=0; i<n; i++){
            if(visited[i]!=2 && prComplete(i, visited, prereqs, false)){
                cout<<"Starting with "<<i<<endl;
                dfs(n, adj, courses, i, visited, prereqs);
            }
        }
        for(int i=0; i<courses.size(); i++){
            cout<<courses[i]<<" ";
        }
        // reverse(courses.begin(), courses.end());
        return courses.size()==n ? courses : vector<int>();
        // return courses.size()==n;
    }
    void dfs(int n, vector<vector<int>>& adj, vector<int>& courses, int start, vector<int>& visited, vector<vector<int>>& prereqs){
        if(dfs_visit(start, adj, courses, visited, prereqs)==false){
            return;
        }
        return;
    }
    bool dfs_visit(int s, vector<vector<int>>& adj, vector<int>& courses, vector<int>& visited, vector<vector<int>>& prereqs){
        visited[s] = 1;
        courses.push_back(s);
        for(int i=0; i<adj[s].size(); i++){
            int x = adj[s][i];
            if(visited[x]==0 && prComplete(x, visited, prereqs, true)){
                cout<<"visiting "<<x<<endl;
                visited[x]=1;
                // courses.push_back(x);
                dfs_visit(x, adj, courses, visited, prereqs);
            }else if(visited[x]==1){
                return false;
            }
        }
        visited[s]=2;
        // courses.push_back(s);
        return true;
    }
    bool prComplete(int n, vector<int>& visited, vector<vector<int>>& prc, bool inQueueAllowed){
        for(int i=0; i<prc[n].size(); i++){
            cout<<" pr of "<<n<<" is "<<prc[n][i]<< " status is "<<visited[prc[n][i]]<<endl;
            if(visited[prc[n][i]] <= (inQueueAllowed ? 0 : 1)) return false;
        }
        return true;
    }
};
18600