c++ | easy | short
\n\n# Code\n\nclass Solution {\npublic:\n   vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n    vector<vector<int>> ans(n), directChild(n
\n\n# Code\n```\nclass Solution {\npublic:\n   vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n    vector<vector<int>> ans(n), directChild(n);\n    for(auto& e: edges) \n        directChild[e[0]].push_back(e[1]);\n    for(int i = 0; i < n; i++)\n        dfs(i, i, ans, directChild);\n    return ans;\n}\nvoid dfs(int x, int curr, vector<vector<int>>& ans, vector<vector<int>>& directChild) {\n    for (auto& ch: directChild[curr]) \n        if(ans[ch].size() == 0 || ans[ch].back() != x) {\n            ans[ch].push_back(x);\n            dfs(x, ch, ans, directChild);\n        }\n}\n};\n```
['C++']