Took me few days to think but never came up with an approach
\tclass Solution {\n\tpublic:\n\t\tvoid dfs(unordered_map> &mp,vector &visited, int ancestor){\n\t\t\tif(visited[ancestor] == true)   return;\n\t\t\tvisited[anc
\tclass Solution {\n\tpublic:\n\t\tvoid dfs(unordered_map<int,vector<int>> &mp,vector<bool> &visited, int ancestor){\n\t\t\tif(visited[ancestor] == true)   return;\n\t\t\tvisited[ancestor] = true;\n\t\t\tfor(int i = 0; i<mp[ancestor].size(); i++){\n\t\t\t\tif(visited[mp[ancestor][i]] == false){\n\t\t\t\t\tdfs(mp,visited,mp[ancestor][i]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n\t\t\tunordered_map<int,vector<int>> mp;\n\t\t\tfor(int i = 0; i<edges.size(); i++)  mp[edges[i][1]].push_back(edges[i][0]);\n\t\t\tvector<vector<int>> answer;\n\t\t\tfor(int i = 0; i<n; i++){\n\t\t\t\tvector<bool> visited(n,false);\n\t\t\t\tvector<int> temp;\n\t\t\t\tdfs(mp,visited,i);\n\t\t\t\tfor(int j = 0; j<n; j++){\n\t\t\t\t\tif(visited[j] == true && i!=j)  temp.push_back(j);\n\t\t\t\t}\n\t\t\t\tanswer.push_back(temp);\n\t\t\t}\n\t\t\treturn answer;\n\t\t}\n\t};
[]