题解 | #检测循环依赖#
检测循环依赖
https://www.nowcoder.com/practice/8dc02ad98553432a90affc3a0484910b
class Solution { public: vector<int> findOrder(vector<vector<int> >& prerequisites, int n) { vector<int> ans; set<int> tmp; map<int, int> dic; int i,c = 0; for(i = 0;i<prerequisites.size();i++) { dic[prerequisites[i][0]]++; dic[prerequisites[i][1]]++; } map<int,int>::iterator it; for(it = dic.begin();it!=dic.end();it++) { if(it->second % 2==0) c++; tmp.insert(it->first); } if(c==n) return ans; set<int>::iterator s; for(s = tmp.begin();s!=tmp.end();s++) { ans.push_back(*s); } if(ans.size()==n) return ans; sort(ans.begin(), ans.end()); for(i=ans.size();i<n;i++) { ans.push_back(ans[i-1]+1); } return ans; } };