public boolean canFinish(int numCourses, int[][] prerequisites) {
        List<List<Integer>> lists = new ArrayList<List<Integer>>(numCourses);
        for (int i = 0; i < numCourses; ++i) {
            List<Integer> tmp = new LinkedList<Integer>();
            lists.add(tmp);
        }
        for (int i = 0; i < prerequisites.length; ++i) {
            lists.get(prerequisites[i][1]).add(prerequisites[i][0]);
        }

        boolean[] visited = new boolean[numCourses];
        int begin = 0;
        while (begin < numCourses) {
            if (isLoop(begin, lists, visited))
                return false;
            while (begin < numCourses && lists.get(begin++).isEmpty());
        }
        return true;
    }