Easy C++ Solution With Video Explanation
Video Explanation\nhttps://youtu.be/ui4qbDTdPd4\n\n# Complexity\n- Time complexity: O(n^2)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(
# Video Explanation\nhttps://youtu.be/ui4qbDTdPd4\n\n# Complexity\n- Time complexity: $$O(n^2)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n^2)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n        vector<vector<int>>adj(n);\n        vector<int>inDegree(n,0);\n        for(auto it:edges){\n            adj[it[0]].push_back(it[1]);\n            inDegree[it[1]]++;\n        }\n        queue<int>q;\n        for(int i=0;i<n;i++){\n            if(inDegree[i]==0){\n                q.push(i);\n            }\n        }\n        vector<set<int>>ans(n);\n        while(!q.empty()){\n            int node=q.front();\n            q.pop();\n            for(auto adjnode:adj[node]){\n                inDegree[adjnode]--;\n                ans[adjnode].insert(node);\n                if(inDegree[adjnode]==0){\n                    q.push(adjnode);\n                }\n                for(auto ancestor:ans[node]){\n                    ans[adjnode].insert(ancestor);\n                }\n            }\n        }\n        vector<vector<int>>res(n);\n        for(int i=0;i<n;i++){\n            res[i]=vector<int>(ans[i].begin(),ans[i].end());\n        }\n        return res;\n    }\n};\n```
['C++']