Solution By Dare2Solve | Detailed Explanation | Clean Code
Explanation []\nauthorslog.com/blog/a89c2S0zHB\n\n# Code\n\ncpp []\nclass Solution {\npublic:\n    int minReorder(int n, vector<vector<int>>& connections) {\n  
```Explanation []\nauthorslog.com/blog/a89c2S0zHB\n```\n# Code\n\n```cpp []\nclass Solution {\npublic:\n    int minReorder(int n, vector<vector<int>>& connections) {\n        vector<vector<int>> grid(n);\n        vector<int> vis(n, 0);\n\n        for (const auto& conn : connections) {\n            int src = conn[0];\n            int dest = conn[1];\n            grid[src].push_back(-1 * dest);\n            grid[dest].push_back(src);\n        }\n\n        return solve(grid, 0, vis);\n    }\n\nprivate:\n    int solve(vector<vector<int>>& grid, int val, vector<int>& vis) {\n        if (vis[val] == 1) {\n            return 0;\n        }\n        int ans = 0;\n        vis[val] = 1;\n        for (int ele : grid[val]) {\n            if (vis[abs(ele)] == 1) {\n                continue;\n            }\n            if (ele < 0) {\n                ans += 1;\n            }\n            ans += solve(grid, abs(ele), vis);\n        }\n        return ans;\n    }\n};\n```\n\n```python []\nclass Solution:\n    def minReorder(self, n, connections):\n        grid = [[] for _ in range(n)]\n        vis = [False] * n\n\n        for src, dest in connections:\n            grid[src].append(-dest)\n            grid[dest].append(src)\n\n        return self.solve(grid, 0, vis)\n\n    def solve(self, grid, val, vis):\n        if vis[val]:\n            return 0\n        ans = 0\n        vis[val] = True\n        for ele in grid[val]:\n            if vis[abs(ele)]:\n                continue\n            if ele < 0:\n                ans += 1\n            ans += self.solve(grid, abs(ele), vis)\n        return ans\n\n# Example usage:\nsol = Solution()\nn = 6\nconnections = [[0, 1], [1, 3], [2, 3], [4, 0], [4, 5]]\nprint(sol.minReorder(n, connections))\n```\n\n```java []\npublic class Solution {\n\n    private int solve(List<List<Integer>> grid, int val, boolean[] vis) {\n        if (vis[val]) {\n            return 0;\n        }\n        int ans = 0;\n        vis[val] = true;\n        for (int ele : grid.get(val)) {\n            if (vis[Math.abs(ele)]) {\n                continue;\n            }\n            if (ele < 0) {\n                ans += 1;\n            }\n            ans += solve(grid, Math.abs(ele), vis);\n        }\n        return ans;\n    }\n\n    public int minReorder(int n, int[][] connections) {\n        List<List<Integer>> grid = new ArrayList<>();\n        boolean[] vis = new boolean[n];\n\n        for (int i = 0; i < n; i++) {\n            grid.add(new ArrayList<>());\n        }\n\n        for (int[] conn : connections) {\n            int src = conn[0];\n            int dest = conn[1];\n            grid.get(src).add(-1 * dest);\n            grid.get(dest).add(src);\n        }\n\n        return solve(grid, 0, vis);\n    }\n\n    public static void main(String[] args) {\n        Solution solution = new Solution();\n        int n = 6;\n        int[][] connections = {{0, 1}, {1, 3}, {2, 3}, {4, 0}, {4, 5}};\n        System.out.println(solution.minReorder(n, connections));\n    }\n}\n```\n\n```javascript []\nvar minReorder = function (n, connections) {\n    let adj = [], grid = [], vis = [], len = connections.length;\n    for (let i = 0; i < n; i++) {\n        grid.push([]);\n        vis[i] = 0;\n    }\n    for (let i = 0; i < len; i++) {\n        let src = connections[i][0];\n        let dest = connections[i][1];\n        grid[src].push(-1 * dest);\n        grid[dest].push(src);\n    }\n    console.log(grid);\n    return solve(grid, 0, vis);\n};\nfunction solve(grid, val, vis) {\n    if (vis[val] === 1) {\n        return 0;\n    }\n    let ans = 0;\n    vis[val] = 1;\n    for (let i = 0; i < grid[val].length; i++) {\n        let ele = grid[val][i];\n        if (vis[Math.abs(ele)] == 1) {\n            continue;\n        }\n        if (ele < 0) {\n            ans += 1;\n        }\n        ans += solve(grid, Math.abs(ele), vis);\n    }\n    return ans;\n}\n```
['Breadth-First Search', 'Graph', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']