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];
            Cycle cycle = new Cycle();
            dfs(graph, i, visited, onStack, cycle);
            if (!cycle.cycle) {
                for (int j = 0; j < visited.length; j++) {
                    if (visited[j] && !ignored[j]) {
                        ignored[j] = true;
                        result.add(j);
                    }
                }
            }
        }
        Collections.sort(result);
        return result;
    }

    private static class Cycle {
        private boolean cycle;
    }

    private static void dfs(int[][] graph, int source, boolean[] visited,
                            boolean[] onStack, Cycle cycle) {
        visited[source] = true;
        onStack[source] = true;
        for (int neighbor : graph[source]) {
            if (!visited[neighbor]) {
                dfs(graph, neighbor, visited, onStack, cycle);
            } else {
                if (onStack[neighbor]) {
                    cycle.cycle = true;
                }
            }
        }
        onStack[source] = false;
    }

    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}
        })); // []
    }
}

@@ -581,21 +581,23 @@
         
-Cycle
+boolean
  cycle =
@@ -601,37 +601,17 @@
 e = 
-new 
+has
 Cycle(
-);%0A            dfs(
 grap
@@ -624,39 +624,32 @@
 visited, onStack
-, cycle
 );%0A            i
@@ -657,22 +657,16 @@
  (!cycle
-.cycle
 ) %7B%0A    
@@ -887,32 +887,233 @@
               %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  
@@ -1207,87 +1207,24 @@
 tic 
-class Cycle %7B%0A        private boolean cycle;%0A    %7D%0A%0A    private static void dfs
+boolean hasCycle
 (int
@@ -1286,32 +1286,40 @@
                 
+        
 boolean%5B%5D onStac
@@ -1323,21 +1323,8 @@
 tack
-, Cycle cycle
 ) %7B%0A
@@ -1379,32 +1379,63 @@
 source%5D = true;%0A
+        boolean cycle = false;%0A
         for (int
@@ -1521,11 +1521,25 @@
     
-dfs
+cycle %7C= hasCycle
 (gra
@@ -1572,15 +1572,8 @@
 tack
-, cycle
 );%0A 
@@ -1657,21 +1657,14 @@
     
-cycle.cycle =
+return
  tru
@@ -1737,24 +1737,46 @@
 e%5D = false;%0A
+        return cycle;%0A
     %7D%0A%0A    p

Update problem 802