Easy C++ Solution ✅✅ || Toposort || BFS
Approach\n1. Create adjacency list for given graph.\n2. Perform topological sort.\n3. While performing toposort for each adjacent node insert its parent in ans 
# Approach\n1. Create adjacency list for given graph.\n2. Perform topological sort.\n3. While performing toposort for each adjacent node insert its parent in ans and insert parent\'s parents in ans. (Check if they are not already present)\n4. sort each vector in ans.\n5. return ans.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:$$O(n+m)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(n*m)$$ (to store answer)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution\n{\npublic:\n\tvector<vector<int>> getAncestors(int n, vector<vector<int>> &edges)\n\t{\n\t\tvector<int> adj[n];\n\t\tint m = edges.size();\n\t\tfor (int i = 0; i < m; i++)\n\t\t{\n\t\t\tadj[edges[i][0]].push_back(edges[i][1]);\n\t\t}\n\t\tvector<int> indeg(n, 0);\n\t\tfor (int i = 0; i < n; i++)\n\t\t{\n\t\t\tfor (auto adjnode : adj[i])\n\t\t\t{\n\t\t\t\tindeg[adjnode]++;\n\t\t\t}\n\t\t}\n\t\tqueue<int> q;\n\t\tfor (int i = 0; i < n; i++)\n\t\t{\n\t\t\tif (indeg[i] == 0)\n\t\t\t\tq.push(i);\n\t\t}\n\t\tvector<int> toposort;\n\t\tvector<vector<int>> ans(n);\n\t\twhile (!q.empty())\n\t\t{\n\t\t\tint node = q.front();\n\t\t\tq.pop();\n\t\t\ttoposort.push_back(node);\n\t\t\tfor (auto adjnode : adj[node])\n\t\t\t{\n\t\t\t\tindeg[adjnode]--;\n\t\t\t\tif (find(ans[adjnode].begin(), ans[adjnode].end(), node) == ans[adjnode].end())\n\t\t\t\t\tans[adjnode].push_back(node);\n\t\t\t\tfor (int i = 0; i < ans[node].size(); i++)\n\t\t\t\t{\n\t\t\t\t\tif (find(ans[adjnode].begin(), ans[adjnode].end(), ans[node][i]) == ans[adjnode].end())\n\t\t\t\t\t{\n\t\t\t\t\t\tans[adjnode].push_back(ans[node][i]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (indeg[adjnode] == 0)\n\t\t\t\t{\n\t\t\t\t\tq.push(adjnode);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < n; i++)\n\t\t{\n\t\t\tsort(ans[i].begin(), ans[i].end());\n\t\t}\n\t\treturn ans;\n\t}\n};\n```\n![leet_cat.png](https://assets.leetcode.com/users/images/2f114fb1-c8c8-41ee-a2af-d24ba3008174_1706949869.5154023.png)\n
['Breadth-First Search', 'Graph', 'Topological Sort', 'C++']