<p>Given a directed acyclic graph (<strong>DAG</strong>) of <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, find all possible paths from node <code>0</code> to node <code>n - 1</code> and return them in <strong>any order</strong>.</p>

<p>The graph is given as follows: <code>graph[i]</code> is a list of all nodes you can visit from node <code>i</code> (i.e., there is a directed edge from node <code>i</code> to node <code>graph[i][j]</code>).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/28/all_1.jpg" style="width: 242px; height: 242px;" />
<pre>
<strong>Input:</strong> graph = [[1,2],[3],[3],[]]
<strong>Output:</strong> [[0,1,3],[0,2,3]]
<strong>Explanation:</strong> There are two paths: 0 -&gt; 1 -&gt; 3 and 0 -&gt; 2 -&gt; 3.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/28/all_2.jpg" style="width: 423px; height: 301px;" />
<pre>
<strong>Input:</strong> graph = [[4,3,1],[3,2,4],[3],[4],[]]
<strong>Output:</strong> [[0,4],[0,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,4]]
</pre>

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

<ul>
	<li><code>n == graph.length</code></li>
	<li><code>2 &lt;= n &lt;= 15</code></li>
	<li><code>0 &lt;= graph[i][j] &lt; n</code></li>
	<li><code>graph[i][j] != i</code> (i.e., there will be no self-loops).</li>
	<li>All the elements of <code>graph[i]</code> are <strong>unique</strong>.</li>
	<li>The input graph is <strong>guaranteed</strong> to be a <strong>DAG</strong>.</li>
</ul>

const int SIZE = 20;

class Solution {
public:

    int N;
    vector<int> v[SIZE];
    pair<int,int> step[SIZE][SIZE];

    void dfs(int indx, int prev, int dist, vector<vector<int>>& ret){
        step[indx][dist]={dist,prev};
        if(indx==N-1){
            vector<int> temp;
            int idx=indx;
            int back = dist;
            while(idx!=step[idx][back].second || back>0){
                temp.push_back(idx);
                idx = step[idx][back].second;
                back--;
            }
            temp.push_back(0);
            vector<int> ans;
            for(int i=temp.size()-1;i>=0;i--){
                ans.push_back(temp[i]);
            }
            ret.push_back(ans);
        }
        for(int i=0;i<v[indx].size();i++){
            int idx=v[indx][i];
            dfs(idx,indx,dist+1,ret);
        }
    }

    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        vector<vector<int>> ret;
        N=graph.size();
        for(int i=0;i<N;i++){
            int m=graph[i].size();
            for(int j=0;j<m;j++){
                int to=graph[i][j];
                v[i].push_back(to);
            }
        }
        for(int i=0;i<SIZE;i++){
            for(int j=0;j<SIZE;j++){
                step[i][j]={-1,j};
            }
        }
        dfs(0,0,0,ret);
        // queue<pair<int,int> > q;
        // q.push({0,0});
        // step[0][0]={0,0};
        // while(!q.empty()){
        //     int indx=q.front().first;
        //     int dist=q.front().second;
        //     q.pop();
        //     if(indx==n-1){
        //         vector<int> temp;
        //         int idx=indx;
        //         int back = dist;
        //         while(idx!=step[idx][back].second || back>0){
        //             temp.push_back(idx);
        //             idx = step[idx][back].second;
        //             back--;
        //         }
        //         temp.push_back(0);
        //         vector<int> ans;
        //         for(int i=temp.size()-1;i>=0;i--){
        //             ans.push_back(temp[i]);
        //         }
        //         ret.push_back(ans);
        //     }
        //     for(int i=0;i<v[indx].size();i++){
        //         int idx=v[indx][i];
        //         if(step[idx][dist+1].first==-1){
        //             step[idx][dist+1].first=dist+1;
        //             step[idx][dist+1].second = indx;
        //             q.push({idx,dist+1});
        //         }
        //     }
        // }

        return ret;
    }
};
19808