C++|| Graph || DFS || O(n) || 100% Faster
Intuition\nConsider the graph as undirected graph and then do a DFS considering the src node as 0 and we need to reverse all the forward pointing edges.\nTo kee
# Intuition\nConsider the graph as undirected graph and then do a DFS considering the src node as 0 and we need to reverse all the forward pointing edges.\nTo keep track of real edges I have made adjacency list as the vector<pair<int,int>> so if a real edge is present from a to b then i am inserting it like:-\na -> {b,1}\nb -> {a,0}\n\nbecause b to a is a vitual edge.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nIn the DFS traversal since we are starting from 0 and for all the nodes while traversing its adj list if we come accross a node which is unvisited and the edge to it is real edge then we surely need to reverse that edge so i incremented the count.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n)$$ -> For DFS traversal \n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n) -> $$ for visited array                   $$O(n) -> $$ stack space\n                   $$total space = O(n)+O(n) = O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int c=0;\n    void find(vector<pair<int,int>>adj[],int i,vector<int>&vis){\n        vis[i]=1;\n        for(auto it:adj[i]){\n            if(!vis[it.first]){\n                c+=it.second;\n                find(adj,it.first,vis);\n            }\n        }\n    }\n    int minReorder(int n, vector<vector<int>>& connections) {\n        vector<pair<int,int>>adj[n];\n        for(auto it:connections){\n            adj[it[0]].push_back({it[1],1});\n            adj[it[1]].push_back({it[0],0});\n        }\n        vector<int>vis(n,0);\n        find(adj,0,vis);\n        return c;\n    }  \n};\n```
['Depth-First Search', 'Graph', 'C++']