classSolution{public:vector<vector<int>>getAncestors(int n, vector<vector<int>>& edges){vector<set<int>>ancest(n);vector<int>indegree(n);vector<unordered_set<int>>g(n);for(auto& e : edges){g[e[0]].insert(e[1]);//建圖indegree[e[1]]++;//入度}queue<int> q;for(int i =0; i < n;++i){if(indegree[i]==0)q.push(i);//入度為0加入隊列}while(!q.empty()){int id = q.front();q.pop();for(auto nid : g[id]){for(auto a : ancest[id]){ancest[nid].insert(a);//繼承之前節(jié)點的祖先}ancest[nid].insert(id);//加入from節(jié)點為祖先if(--indegree[nid]==0)q.push(nid);}}vector<vector<int>>ans(n);for(int i =0; i < n;++i){ans[i]= vector<int>(ancest[i].begin(), ancest[i].end());//轉(zhuǎn)化為答案}return ans;}};