Simple, clean and self-explanatory solution
If you know any city x != 0that reaches 0, then you know 0 can be reached (eventually) if you reach the city x. Create a set to keep track of the cities that ca
If you know any city `x != 0 `that reaches 0, then you know 0 can be reached (eventually) if you reach the city x. Create a set to keep track of the cities that can reach 0. As you traverse the connection array, keep adding those cities which has  connection to any city that is in the set. If you found any city which is in the set, and has a connection a city "x" which is not in the set, then that connection has to rerouted, so increment the count.  Repeat this process, until the set size has reached n.\n\n```\npublic int minReorder(int n, int[][] conn) {        \n        HashSet<Integer> set = new HashSet<>();\n        set.add(0);\n        int ans = 0;\n        while(set.size() < n){\n            for(int i=0; i<conn.length; i++){\n                if(set.contains(conn[i][1])){\n                    set.add(conn[i][0]);\n                }else if(set.contains(conn[i][0]) && !set.contains(conn[i][1])){\n                    set.add(conn[i][1]);\n                    ans++;\n                }\n            }\n        }\n        return ans;\n    }\n```\n
[]