[C++] Runtime: 124 ms, faster than 99.84% | Memory: 48.7 MB, less than 99.10%
\nclass Solution {\npublic:\n\tvoid dfs(int i, int anc, vector<vector<int>> &al, vector<vector<int>> &res) {\n\t\tif (res[i].empty() || res[i].back() != anc)  {
```\nclass Solution {\npublic:\n\tvoid dfs(int i, int anc, vector<vector<int>> &al, vector<vector<int>> &res) {\n\t\tif (res[i].empty() || res[i].back() != anc)  {\n\t\t\tif (i != anc)\n\t\t\t\tres[i].push_back(anc);\n\t\t\tfor (auto j : al[i])\n\t\t\t\tdfs(j, anc, al, res);\n\t\t}\n\t}\n\tvector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n\t\tvector<vector<int>> res(n), al(n);\n\t\tfor (auto &e: edges)\n\t\t\tal[e[0]].push_back(e[1]);\n\t\tfor (int i = 0; i < n; ++i)\n\t\t\tdfs(i, i, al, res);\n\t\treturn res;    \n\t}\n};\n```
['Depth-First Search', 'Graph', 'C']