JAVA | BFS | Explained Solution
Here we\'ll simply construct out graph and also maintain a hashset where we\'ll store the original edges given to us. So when we\'ll start our normal BFS with 0
Here we\'ll simply construct out graph and also maintain a hashset where we\'ll store the original edges given to us. So when we\'ll start our normal BFS with 0 as starting node whenever we find a edge that is taking as away from our parent we\'ll check if that exits in our hashset and if yes then we\'ll increase our answer count. \n\n```\nclass Solution {\n    public int minReorder(int n, int[][] connections) {\n        HashSet<String> set = new HashSet<>();\n        ArrayList<Integer>[] graph = new ArrayList[n];\n        for(int i = 0 ; i < n ; i++) {\n            graph[i] = new ArrayList<>();\n        }\n        for(int[] e : connections) {\n            int u = e[0], v = e[1];\n            graph[u].add(v);\n            graph[v].add(u);\n            set.add(u + "->" + v);\n        }\n        Queue<Integer> q = new LinkedList<>();\n        q.add(0);\n        int res = 0;\n        boolean[] vis = new boolean[n];\n        while(!q.isEmpty()) {\n            int size = q.size();\n            while(size-- > 0) {\n                int rem = q.remove();\n                vis[rem] = true;\n                for(int e : graph[rem]) {\n                    if(!vis[e]) {\n                        if(set.contains(rem + "->" + e)) res++;\n                        q.add(e);   \n                    }\n                }\n            }\n        }\n        return res;\n    }\n}\n```
['Depth-First Search', 'Breadth-First Search', 'Java']