[C++]/DFS CODE WITH DETAILED EXPLAINATION AND MINIMUM TIME COMPLEXITY ✅✅✅
Intuition\n- First, we can think of counting the total number of edges that are not pointing towards zero because that will give us our final answer.\n- However
# Intuition\n- First, we can think of counting the total number of edges that are not pointing towards zero because that will give us our final answer.\n- However, the question here is how to count these edges? Let\'s discuss the approach. Before proceeding i will suggest to think once again with this intuition.\n\n# Approach\n- First, we create an adjacency list, similar to what we do for an undirected graph.\n- Now, we can store the directions of all the edges in a map. For example, if there is an edge from u to v (u -> v), we can set map[{u, v}] = 1. We will use this map to determine the direction of each edge as we proceed further.\n- Now, we start a depth-first search (DFS) from 0 and count the edges that are directed from the parent to the child. These edges are pointing in the opposite direction of the 0 node, whether it is the direct or indirect parent of the current node.\n- Once the DFS traversal is completed, we will have the count of edges that are not pointing towards 0. This count represents our final answer.\n\n# Complexity\n- Time complexity: O(V*log(E)) V=no of vertices,E=no of edges,\nO(V) is for dfs and log(E) time for checking the direction of edge in map in dfs.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(V+E)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int ans=0;\n    void dfs(vector<int> gr[],map<pair<int,int>,int> &direction,int node,int par){\n        for(auto it: gr[node]){\n            if(it==par) continue;\n            if(direction.find({node,it})!=direction.end()) ans++;\n            dfs(gr,direction,it,node);\n        }\n    }\n    int minReorder(int n, vector<vector<int>>& conn) {\n        vector<int> gr[n];\n        map<pair<int,int>,int> direction;\n        for(auto it: conn){\n            gr[it[0]].push_back(it[1]);\n            gr[it[1]].push_back(it[0]);\n            direction[{it[0],it[1]}]=1;\n        }\n\n        dfs(gr,direction,0,-1);\n        return ans;\n    }\n};\n```\n# **PLEASE UPVOTE THE SOLUTION**\n![upvote.jpg](https://assets.leetcode.com/users/images/ca1d5ba8-0e91-496a-84cc-351cebad6993_1687923152.6429424.jpeg)\n
['C++']