JavaScript : Detailed explanation
Code\n\nvar minReorder = function(n, connections) {\n    // from: (<from city>, [<to cities>])\n    // to: (<to city>, [<from cities>])\n    const from = new Ma
# Code\n```\nvar minReorder = function(n, connections) {\n    // from: (<from city>, [<to cities>])\n    // to: (<to city>, [<from cities>])\n    const from = new Map(), to = new Map();\n\n    // Function to insert in values in map\n    const insert = (map, key, value) => {\n        if(map.has(key)){\n            const arr = map.get(key);\n            arr.push(value);\n            map.set(key, arr);\n        } else {\n            map.set(key, [value]);\n        }\n    }\n\n    // Set all values in both maps\n    for(const [a,b] of connections){\n        insert(from, a, b);\n        insert(to, b, a);\n    }\n\n    // Queue: cities to visit\n    const queue = [0], visited = new Set();\n    let count = 0;\n\n    while(queue.length) {\n        const cur = queue.shift(); // First element in queue\n\n        // Check values in first map\n        if(from.has(cur)){\n            for(const x of from.get(cur)){\n                // If visited, do nothing else add to queue\n                if(visited.has(x)) continue;\n                queue.push(x);\n                count++; // Change direction of this path\n            }\n        }\n\n        if(to.has(cur)){\n            // If visited, do nothing else add to queue\n            for(const x of to.get(cur)){\n                if(visited.has(x)) continue;\n                queue.push(x);\n            }\n        }\n\n        visited.add(cur); // Mark city as visited\n    }\n\n    return count\n};\n```
['JavaScript']