DFS using hashset and hashmaps.
Intuition\n Describe your first thoughts on how to solve this problem. \nDepth First Search.\n# Approach\n Describe your approach to solving the problem. \nReve
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nDepth First Search.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nReverse the graph and check its adjacency list. If it\'s not visited apply recursion on that (adjnode) and append it the set of the current call. If it is visited find its corresponding set in the map and append it to the set of the current call.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(V+E)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(V+E)\n# Code\n```\nclass Solution {\npublic:\n    map<int,set<int>> m ;\n\n    set<int> dfs(int node,vector<vector<int>> &adj,vector<int>&visited){\n        \n        set<int> s;\n        visited[node]=1;\n       \n        s.insert(node);\n\n        for(auto adjnode : adj[node]){\n            if (!visited[adjnode]){\n                set<int> back = dfs(adjnode,adj,visited);\n                s.insert(back.begin(),back.end());\n            }\n            else{\n                set<int> vis = m[adjnode];\n                s.insert(vis.begin(),vis.end());\n            }\n        }\n        m[node]=s;\n        return s;\n    }\n\n    vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n         vector<vector<int>> adj(n);\n\n         for (auto i : edges){\n             adj[i[1]].push_back(i[0]);\n         }\n\n         vector<vector<int>> ans;\n\n         vector<int>visited(n,0);\n\n        for(int i  = 0 ;  i <n ; i++){\n            if (!visited[i]){\n                dfs(i,adj,visited);\n            }\n        }\n\n        for (int i = 0 ; i < n ; i++){\n            vector<int> temp;\n            for(int k : m[i]){\n               if ( k!=i) \n                temp.push_back(k);\n            }\n            ans.push_back(temp);\n        }\n\n        return ans;\n    }\n};\n```
['C++']