package leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * https://leetcode.com/problems/find-eventual-safe-states/
 */
public class Problem802 {
    public List<Integer> eventualSafeNodes(int[][] graph) {
        List<Integer> result = new ArrayList<>();
        boolean[] ignored = new boolean[10000];
        for (int i = 0; i < graph.length; i++) {
            if (ignored[i]) {
                continue;
            }
            boolean[] visited = new boolean[10000];
            boolean[] onStack = new boolean[10000];
            boolean cycle = hasCycle(graph, i, visited, onStack);
            if (!cycle) {
                for (int j = 0; j < visited.length; j++) {
                    if (visited[j] && !ignored[j]) {
                        ignored[j] = true;
                        result.add(j);
                    }
                }
            } else {
                for (int j = 0; j < onStack.length; j++) {
                    if (onStack[j]) {
                        ignored[j] = true;
                    }
                }
            }
        }
        Collections.sort(result);
        return result;
    }

    private static boolean hasCycle(int[][] graph, int source, boolean[] visited,
                                    boolean[] onStack) {
        visited[source] = true;
        onStack[source] = true;
        boolean cycle = false;
        for (int neighbor : graph[source]) {
            if (!visited[neighbor]) {
                cycle |= hasCycle(graph, neighbor, visited, onStack);
            } else {
                if (onStack[neighbor]) {
                    return true;
                }
            }
        }
        onStack[source] = false;
        return cycle;
    }

    public static void main(String[ ] args) {
        Problem802 prob = new Problem802();
        System.out.println(prob.eventualSafeNodes(new int[][]{
            {1, 2}, {2, 3}, {5}, {0}, {5}, {}, {}
        })); // [2,4,5,6]
        System.out.println(prob.eventualSafeNodes(new int[][]{
            {1, 2}, {1}, {0}
        })); // []
    }
}

@@ -44,38 +44,8 @@
 st;%0A
-import java.util.Collections;%0A
 impo
@@ -214,24 +214,134 @@
 %5B%5D graph) %7B%0A
+        boolean%5B%5D onStack = new boolean%5Bgraph.length%5D;%0A        boolean%5B%5D visited = new boolean%5Bgraph.length%5D;%0A
         List
@@ -382,56 +382,8 @@
 ();%0A
-        boolean%5B%5D ignored = new boolean%5B10000%5D;%0A
     
@@ -443,754 +443,142 @@
     
-if (ignored%5Bi%5D) %7B%0A                continue;%0A            %7D%0A            boolean%5B%5D visited = new boolean%5B10000%5D;%0A            boolean%5B%5D onStack = new boolean%5B10000%5D;%0A            boolean cycle = hasCycle(graph, i, visited, onStack);%0A            if (!cycle) %7B%0A                for (int j = 0; j %3C visited.length; j++) %7B%0A                    if (visited%5Bj%5D && !ignored%5Bj%5D) %7B%0A                        ignored%5Bj%5D = true;%0A                        result.add(j);%0A                    %7D%0A                %7D%0A            %7D else %7B%0A                for (int j = 0; j %3C onStack.length; j++) %7B%0A                    if (onStack%5Bj%5D) %7B%0A                        ignored%5Bj%5D = true;%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A        Collections.sort(result);
+boolean cycle = hasCycle(graph, i, visited, onStack);%0A            if (!cycle) %7B%0A                result.add(i);%0A            %7D%0A        %7D
 %0A   
@@ -811,39 +811,8 @@
 ue;%0A
-        boolean cycle = false;%0A
     
@@ -910,15 +910,22 @@
     
+boolean 
 cycle 
-%7C
 = ha
@@ -959,32 +959,112 @@
 ited, onStack);%0A
+                if (cycle) %7B%0A                    return true;%0A                %7D%0A
             %7D el
@@ -1232,20 +1232,20 @@
  return 
-cycl
+fals
 e;%0A    %7D

Solve problem 802