Java (Using Queue | simple dfs)
First approach:(Using a queue)\n1. Make a boolean array hasReached[] where hasReached[i] means ith vertex can now reach 0.\n2. Push all edges in a queue\n3. Pop
First approach:(Using a queue)\n1. Make a boolean array hasReached[] where hasReached[i] means ith vertex can now reach 0.\n2. Push all edges in a queue\n3. Pop queue, see if in the edge (u,v) we have a node that reaches 0.( hasReached[u] or hasReached[v] ) \n4. Let the node that reaches 0 be \'u\'.\n5. So now for \'v\' to reach 0 it has to go through \'u\'. So we have to change edge u->v to v->u. Hence we increment the answer as we changed orientation. Also mark hasReached[v] = true.\n6. If \'v\' was reachable i.e. hasReached[v] == true , then there is no need to change orientation as \'u\' can reach \'v\' and from there 0.  Mark hasReached[u] = true\n7. If none of the vertices in the current edge were leading to 0 push them back in queue.\n8. Continue till the queue is empty. \n```\nclass Solution {\n    static class Pair{\n        public int first;\n        public int second;\n        public Pair(int first, int second){\n            this.first = first;\n            this.second = second;\n        }\n    }\n    public int minReorder(int n, int[][] connections) {\n        LinkedList<Pair> q = new LinkedList<>();\n        for(int i=0;i<n-1;i++){\n            q.add(new Pair(connections[i][0], connections[i][1]));\n        }\n        boolean[] hasReached = new boolean[n];\n        hasReached[0] = true;\n        int ans = 0;\n        while(q.size()>0){\n            Pair temp = q.get(0);\n            q.removeFirst();\n            if(hasReached[temp.first]){\n                hasReached[temp.second] = true;\n                ans++;\n            }\n            else if(hasReached[temp.second]){\n                hasReached[temp.first] = true;\n            }else{\n                q.add(temp);\n            }\n        }\n        return ans;\n    }\n}\n```\n\nSecond approach: (Simple dfs in undirected graph)\n1. Make an adjacency list , but make the graph undirected.\n2. Make a parent array of size n, where parent[i] is parent of i , when the tree is rooted at zero.\n3. Do a dfs with 0 as root and fill the parent array\n4. Now as we know who is parent of whom traverse the given edges and see what edges are from parent to child in original directed graph. These edges have to be changed, hence increment the answer everytime you find such an edge.\n\n```\nclass Solution {\n    public int minReorder(int n, int[][] connections) {\n        ArrayList<ArrayList<Integer>> adj = new ArrayList<>();\n        for(int i=0;i<n;i++){\n            adj.add(new ArrayList<Integer>());\n        } \n        \n        for(int i=0;i<n-1;i++){\n            adj.get(connections[i][0]).add(connections[i][1]);\n            adj.get(connections[i][1]).add(connections[i][0]);\n        }\n        \n        int[] parent = new int[n];\n        parent[0] = -1;        \n        dfs(adj,0,-1,parent);\n        \n        int ans = 0;\n        for(int i=0;i<n-1;i++){\n            int child = connections[i][0];\n            int pat = connections[i][1];\n            if(parent[child]!=pat){\n                ans++;\n            }\n        }\n          \n        return ans;\n    }\n    \n    private static void dfs(ArrayList<ArrayList<Integer>> adj,int source, int p, int[] parent){\n        for(int s: adj.get(source)){\n            if(s!=p){\n                parent[s] = source;\n                dfs(adj,s,source,parent);\n            }\n        }\n    }\n}\n```
['Depth-First Search', 'Queue', 'Java']