<p>There is a directed graph of <code>n</code> nodes with each node labeled from <code>0</code> to <code>n - 1</code>. The graph is represented by a <strong>0-indexed</strong> 2D integer array <code>graph</code> where <code>graph[i]</code> is an integer array of nodes adjacent to node <code>i</code>, meaning there is an edge from node <code>i</code> to each node in <code>graph[i]</code>.</p>

<p>A node is a <strong>terminal node</strong> if there are no outgoing edges. A node is a <strong>safe node</strong> if every possible path starting from that node leads to a <strong>terminal node</strong> (or another safe node).</p>

<p>Return <em>an array containing all the <strong>safe nodes</strong> of the graph</em>. The answer should be sorted in <strong>ascending</strong> order.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="Illustration of graph" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/03/17/picture1.png" style="height: 171px; width: 600px;" />
<pre>
<strong>Input:</strong> graph = [[1,2],[2,3],[5],[0],[5],[],[]]
<strong>Output:</strong> [2,4,5,6]
<strong>Explanation:</strong> The given graph is shown above.
Nodes 5 and 6 are terminal nodes as there are no outgoing edges from either of them.
Every path starting at nodes 2, 4, 5, and 6 all lead to either node 5 or 6.</pre>

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

<pre>
<strong>Input:</strong> graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]]
<strong>Output:</strong> [4]
<strong>Explanation:</strong>
Only node 4 is a terminal node, and every path starting at node 4 leads to node 4.
</pre>

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

<ul>
	<li><code>n == graph.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
	<li><code>0 &lt;= graph[i].length &lt;= n</code></li>
	<li><code>0 &lt;= graph[i][j] &lt;= n - 1</code></li>
	<li><code>graph[i]</code> is sorted in a strictly increasing order.</li>
	<li>The graph may contain self-loops.</li>
	<li>The number of edges in the graph will be in the range <code>[1, 4 * 10<sup>4</sup>]</code>.</li>
</ul>


class Solution {
  public:
    vector<bool> marked;
    vector<bool> finished;
    unordered_set<int> cycle;
    bool is_cycle = false;
    vector<int> ans;

    void DFS(vector<vector<int>>& graph, int start, vector<int>& path) {
        if (marked[start])
            return;

        marked[start] = true;
        path.push_back(start);

        for (auto& n : graph[start]) {
            DFS(graph, n, path);
            if (is_cycle) {
                return;
            }

            if (!finished[n]) {
                is_cycle = true;
                cycle.insert(path.begin(), path.end());
                return;
            }
        }

        ans.push_back(start);
        path.pop_back();

        finished[start] = true;
    }

    vector<int> eventualSafeNodes(vector<vector<int>>& graph) {
        int n = graph.size();
        marked = vector(n, false);
        finished = vector(n, false);
        // vector<int> outdegree(n);
        // set<int> terminal;

        // for (int i = 0; i < n; ++i) {
        //     if (graph[i].size() == 0) {
        //         terminal.insert(i);
        //     }
        // }

        // queue<int> q;
        // vector<int> indegree(n);
        // for (int i = 0; i < n; ++i) {
        //     for (int j = 0; j < graph[i].size(); ++i) {
        //         indegree[graph[i][j]]++;
        //     }
        // }
        //
        // for (int i = 0; i < n; ++i) {
        //     if (indegree[i] == 0) {
        //         q.push(i);
        //     }
        // }
        //
        // while (!q.empty()) {
        //     auto curr = q.front();
        //     q.pop();
        //
        //     for (auto& n : graph[curr]) {
        //         if (--indegree[n] == 0) {
        //             q.push(n);
        //         }
        //     }
        // }

        // vector<int> ans;
        // for (int i = 0; i < n; ++i) {
        //     if (indegree[i] == 0) {
        //         ans.push_back(i);
        //     }
        // }
        //
        // return ans;

        vector<int> path;
        for (int i = 0; i < n; ++i) {
            if (!cycle.contains(i)) {
                is_cycle = false;
                DFS(graph, i, path);
            }
        }

        // vector<int> ans;
        //
        // for (int i = 0; i < n; ++i) {
        //     if (!cycle.contains(i)) {
        //         ans.push_back(i);
        //     }
        // }
        ranges::sort(ans);
        return ans;
    }
};

57396