[BFS + DFS]  Use DFS  to explore and hashset to tracking old road orientation and trick
idea: traverse tree(or rode network) and count reversing\nTime Complexity:  O(N)\nSpace Complexity: O(N)\nAlthough I solved 700+ leetcode problems, I am still r
idea: traverse tree(or rode network) and count reversing\nTime Complexity:  O(N)\nSpace Complexity: O(N)\nAlthough I solved 700+ leetcode problems, I am still rusty about some grammer. So I honestly documented all the bugs before deriving the final accepted solution. Wish it is helpful to the community. \n\n\n```\n// idea: traverse tree(or rode network) and count reversing\nclass Solution {\npublic:\n    int minReorder(int n, vector<vector<int>>& connections) {\n        G_ = vector<vector<int>>(n);\n        for(auto& e: connections){\n            G_[e[0]].push_back(e[1]);\n            G_[e[1]].push_back(e[0]);\n            //paths_.insert(e);\n            paths_.insert(e[0] << 16 | e[1]);\n        }\n        \n        seen_ = vector<int> (n, 0);\n        seen_[0]=1;\n        ans_ = 0;\n        dfs(0);\n        return ans_;\n    }\n    \nprivate:\n    int ans_;\n    vector<int> seen_;\n    //unordered_set<vector<int>> paths_; // bug2\n    //set<vector<int>> paths_;           // bug1\n    unordered_set<int> paths_; //Note1:\n    vector<vector<int>> G_;\n    void dfs(int cur){             \n        for(auto nxt: G_[cur]){   \n            if(seen_[nxt]) continue;\n            seen_[nxt] = 1;\n            dfs(nxt);\n            //if(paths_.count({cur, nxt})) ans_++;\n            if(paths_.count(cur << 16 | nxt)) ans_++;\n        }\n    }\n};\n//Note1:this is a trick to compress state:  key is (from_i << 16 | to_i)\n//without this, it will give TLE error\n```\n\nSolution 2: BFS (Bugged version, please help me to debug)\n```\nclass Solution {\npublic:\n    int minReorder(int n, vector<vector<int>>& conn) {\n        vector<vector<int>> G(n, vector<int>());                \n        for(const auto& e: conn)\n            G[e[0]].push_back(e[1]);\n        \n        queue<int> q;\n        vector<bool> vis(n, false);\n        \n        int count = 0;\n        for(int i = 0; i < n;i++){\n            if(vis[i]) continue;\n            vis[i] = 1;\n            q.push(i);            \n            while(!q.empty())                \n                for(int j = q.size(); j >0; j--){\n                    auto u = q.front(); q.pop();                    \n                    for(auto v: G[u]){\n                        if(vis[v]) continue;                        \n                        vis[v] = true;                        \n                        q.push(v);                        \n                        count++;\n                    }\n                }            \n        }\n        return count;\n    }\n};\n```\n\nNotes about BFS approach:\nExample 1: 5<--4-->0-->1-->3<--2\n\nI will try to explain using the example 1 (given in the question). We would be always starting from node 0 (as given) and if there is any outgoing edge from 0th node then we should count, so this can be done using the BFS as in BFS all edges at level 1 of 0th node will be visited and marked as visited (this are the all wrong path we need to correct, so count this edges for answer). So in 1st iteration 1st node will be visited, now its nearby nodes are visited and count its outgoing edges (so 3rd node will be visited). Now visited array will have visited entry for nodes 0, 1, and 3. The outer loop will insert 2 the queue but it will just mark It as visited(nearby already visited). Now 4 is inserted in the queue and its nearby are 0 and 5 but the key thing to note here is 0 will be not be inserted as it is already visited only 5 will be visited and its wrong direction is counted as answer. I hope this might help you to understand intuition behind the logic.\n\nBased on Example 1, I provide two more variations to provide some more intuition to clear some potetnial doubts I once had:\nExample 1: 5<--4-->0-->1-->3<--2\nExample 4: 0-->1-->3<--2<--4-->5\nExample 5: 0-->1-->3<--2-->4-->5\nNote, the only difference between Example 4 and 5 is direction between city 2 and 4.\n\nThe point in performing BFS is aggregating the count of all unvisited outging edges.  That is it! Or is it? Here is my prove:\nFor all visited nodes/cities, you can simply treat them as one city: city 0. Any city point to city 0 is in right direction and no reverse operation is needed. If you are in univsited City i, and one of your next unvisited cities is j, that means j must be further away from city 0 than i, otherwise, j were already visited before i. In another word, you must visit i first before j.\n\nCredit:\nBFS is modified version of:\nhttps://leetcode.com/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero/discuss/661783/C%2B%2B-Simple-BFS-solution\n\nThanks for warning from: @streamcslive \nThe above soltuion will fail in: \n3\n[[1,2],[2,0]]\n\nSo I provide a new version:\nBFS: (no bug version):\n```\nclass Solution {\npublic:\n    int minReorder(int n, vector<vector<int>>& connections) {\n        int answer=0;\n        vector<vector<int>> G(n);\n        //set<vector<int>> set1;  \n        unordered_set<int> set1;\n        vector<bool> possible(n,false);\n        for(auto i:connections){\n                G[i[0]].push_back(i[1]);\n                G[i[1]].push_back(i[0]);\n                //set1.insert(i);     \n                set1.insert(i[0]<<16 | i[1]); \n        }\n        queue<int> q{{0}};\n        while(!q.empty()){\n            int cur=q.front();  q.pop();\n            if(!possible[cur]){\n                possible[cur]=true;\n                for(auto i:G[cur]){\n                //if(set1.find({cur,i})!=set1.end()&&!possible[i]){\n                if(set1.count(cur << 16 | i) && !possible[i]){\n                    answer++;\n                    q.push(i);\n                    continue;\n                }\n                q.push(i);\n               } \n            } \n        }\n        return answer;\n    }\n};\n```\nIt is a modified version from (I mainly speed up the code):\nhttps://leetcode.com/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero/discuss/669037/C%2B%2B-Using-BFS-easy-explanation
['Depth-First Search', 'Breadth-First Search', 'C']