public boolean canFinish2(int numCourses, int[][] prerequisites) {
        if (numCourses <= 0 || prerequisites == null) return false;
        HashMap<Integer, List<Integer>> graph = new HashMap<Integer, List<Integer>>();
        for (int i = 0; i < numCourses; ++i) graph.put(i, new ArrayList<Integer>());
        for (int[] p : prerequisites) graph.get(p[1]).add(p[0]);

        int[] visited = new int[numCourses];
        for (int n : graph.keySet()) {
            if (!canFinish(graph, visited, n)) return false;
        }
        return true;
    }