There are n cities numbered from 0 to n - 1 and n - 1 roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.

Roads are represented by connections where connections[i] = [ai, bi] represents a road from city ai to city bi.

This year, there will be a big event in the capital (city 0), and many people want to travel to this city.

Your task consists of reorienting some roads such that each city can visit the city 0. Return the minimum number of edges changed.

It's guaranteed that each city can reach city 0 after reorder.

&nbsp;
Example 1:

Input: n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
Output: 3
Explanation: Change the direction of edges show in red such that each node can reach the node 0 (capital).


Example 2:

Input: n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
Output: 2
Explanation: Change the direction of edges show in red such that each node can reach the node 0 (capital).


Example 3:

Input: n = 3, connections = [[1,0],[2,0]]
Output: 0


&nbsp;
Constraints:


	2 &lt;= n &lt;= 5 * 104
	connections.length == n - 1
	connections[i].length == 2
	0 &lt;= ai, bi &lt;= n - 1
	ai != bi


from collections import defaultdict
class Solution:
    def minReorder(self, n: int, connections: List[List[int]]) -> int:
        count, stack, visited = 0, [ 0 ], set() #Add root node to stack
        neighbours = defaultdict(list) #To store neighbours
		adjacency = defaultdict(list) #To store adjacency
        for i, j in connections:
            adjacency[i].append(j)
            neighbours[i].append(j)
            neighbours[j].append(i)
        while stack:
            current = stack.pop()
            if current in visited:
                continue
            else:
                visited.add(current)
            for i in neighbours[current]:
                if i in visited:
                    continue
                if current not in adjacency[i]:
                    count += 1
                stack.append(i)
        return count
class Solution {
    int dfs(List<List<Integer>> al, boolean[] visited, int from) {
        int change = 0;
        visited[from] = true;
        for (var to : al.get(from))
            if (!visited[Math.abs(to)])
                change += dfs(al, visited, Math.abs(to)) + (to > 0 ? 1 : 0);
        return change;   
    }
    public int minReorder(int n, int[][] connections) {
        List<List<Integer>> al = new ArrayList<>();
        for(int i = 0; i < n; ++i) 
            al.add(new ArrayList<>());
        for (var c : connections) {
            al.get(c[0]).add(c[1]);
            al.get(c[1]).add(-c[0]);
        }
        return dfs(al, new boolean[n], 0);
    }
}
class Solution {
public:
    vector<int> Radj[50001],adj[50001] ,visited;
    int bfs(){
        int edges = 0 ;
        queue<int> q ;
        q.push(0) ;

        while(q.size()){
            auto src = q.front() ; q.pop() ;
            visited[src] = 1 ;

            for(auto &nbr : adj[src]){
                if(visited[nbr]) continue ;
                //this connection needs reverse orientation
                ++edges ;
                q.push(nbr) ;
            }

            for(auto &nbr : Radj[src]){
                if(visited[nbr]) continue ;
                q.push(nbr) ;
            }
        }

        return edges ;
    }
    int minReorder(int n, vector<vector<int>>& connections) {
        visited.resize(n,0);
        for(auto &x : connections) adj[x[0]].push_back(x[1]) , Radj[x[1]].push_back(x[0]);
        return bfs() ;
    }
};
var minReorder = function(n, connections) {
    // from: (<from city>, [<to cities>])
    // to: (<to city>, [<from cities>])
    const from = new Map(), to = new Map();

    // Function to insert in values in map
    const insert = (map, key, value) => {
        if(map.has(key)){
            const arr = map.get(key);
            arr.push(value);
            map.set(key, arr);
        } else {
            map.set(key, [value]);
        }
    }

    // Set all values in both maps
    for(const [a,b] of connections){
        insert(from, a, b);
        insert(to, b, a);
    }

    // Queue: cities to visit
    const queue = [0], visited = new Set();
    let count = 0;

    while(queue.length) {
        const cur = queue.shift(); // First element in queue

        // Check values in first map
        if(from.has(cur)){
            for(const x of from.get(cur)){
                // If visited, do nothing else add to queue
                if(visited.has(x)) continue;
                queue.push(x);
                count++; // Change direction of this path
            }
        }

        if(to.has(cur)){
            // If visited, do nothing else add to queue
            for(const x of to.get(cur)){
                if(visited.has(x)) continue;
                queue.push(x);
            }
        }

        visited.add(cur); // Mark city as visited
    }

    return count
};
Reorder Routes to Make All Paths Lead to the City Zero