✅ || DFS || Memorization || C++ || Complexity Analysis
Solution\n\n#### Logic\n\nParent -> Child (TLE)\nChild -> Parent (Accepted)\n\n\ncpp\nclass Solution {\npublic:\n    unordered_map<int, set<int>> me;\n    set<i
## Solution\n\n#### Logic\n\nParent -> Child (TLE)\nChild -> Parent (Accepted)\n\n\n```cpp\nclass Solution {\npublic:\n    unordered_map<int, set<int>> me;\n    set<int> dfs(map<int, vector<int>> &graph, int start) {\n        if (me.find(start) != me.end()) {\n            return me[start];\n        }\n        set<int> parents;\n        for (int i = 0; i < graph[start].size(); i++) {\n            set<int> temp;\n            parents.insert(graph[start][i]);\n            temp = dfs(graph, graph[start][i]);\n            for (auto x : temp) {\n                parents.insert(x);\n            }\n        }\n        me[start] = parents;\n        return parents;\n    }\n    \n    vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n        map<int, vector<int>> graph;\n        vector<vector<int>> sol;\n\n        for (int i = 0; i < edges.size(); i++) {\n            graph[edges[i][1]].push_back(edges[i][0]);\n        }\n        \n        for (int i = 0; i < n; i++) {\n            set<int> temp = dfs(graph, i);\n            sol.push_back({temp.begin(), temp.end()});\n        }\n        \n        \n        return sol;\n    }\n};\n```\n\n## Complexity\n\n __Time Complexity__: O(total_edges * total_edges), because we are using Depth first search. Actully it will be less as we are also using memorization.\n \n __Space Complexity__: O(total_edges * total_edges), becuase we are storing solution to return them also we are making graph in inverted manner so that will also add O(total_edges * total_edges). So 2 * O(total_edges * total_edges) = O(total_edges * total_edges)\n\n<br>\n<br>\n<br>\n\n<p>\n\n __Check out all [my](https://leetcode.com/siddp6/) recent solutions [here](https://github.com/sidd6p/LeetCode)__\n\n \n __Feel Free to Ask Doubts\nAnd Please Share Some Suggestions\nHAPPY CODING :)__\n\n</p>\n\n
['Depth-First Search', 'Memoization', 'C']