Java Simple BFS from Origin
Could be solved by DFS or BFS. BFS is easy to debug, and not causing stackoverflow.\n\n    public int minReorder(int n, int[][] cs) {\n        Set<String> st = 
Could be solved by DFS or BFS. BFS is easy to debug, and not causing stackoverflow.\n```\n    public int minReorder(int n, int[][] cs) {\n        Set<String> st = new HashSet<>();\n        Map<Integer, Set<Integer>> map = new HashMap<>();\n        for (int[] c : cs) {\n            st.add(c[0] + "," + c[1]);\n            map.computeIfAbsent(c[0], k -> new HashSet<>());\n            map.computeIfAbsent(c[1], k -> new HashSet<>());\n            map.get(c[0]).add(c[1]);\n            map.get(c[1]).add(c[0]);\n        }\n        \n        Queue<Integer> q = new LinkedList<>();\n        q.add(0);\n        int res = 0;\n        boolean[] vs = new boolean[n];\n        vs[0] = true;\n        while (!q.isEmpty()) {\n            int c = q.poll();\n            for (int next : map.getOrDefault(c, new HashSet<>())) {\n                if (vs[next]) continue;\n                vs[next] = true;\n                if (!st.contains(next + "," + c)) res++;\n                q.offer(next);\n            }\n        }\n        return res;\n    }\n```
[]