public boolean canFinish(int numCourses, int[][] prerequisites) {
        if (numCourses == 1)
            return true;
        HashMap<Integer, HashSet<Integer>> map = new HashMap<Integer, HashSet<Integer>>();
        for (int i = 0; i < prerequisites.length; ++i) {
            int u = prerequisites[i][1], v = prerequisites[i][0];
            if (map.containsKey(u)) {
                map.get(u).add(v);
            } else {
                HashSet<Integer> set = new HashSet<Integer>();
                set.add(v);
                map.put(u, set);
            }
        }
        HashSet<Integer> visited = new HashSet<Integer>();
        boolean[] flags = new boolean[numCourses];
        for (Map.Entry<Integer, HashSet<Integer>> entry : map.entrySet()) {
            int node = entry.getKey();
            if (flags[node])
                continue;
            if (!dfs(map, flags, node, visited))
                return false;
        }
        return true;
    }