原题连接
Course Schedule题目三连传送门: Course Schedule ii Course Schedule iii
class Solution { public: bool canFinish(int numCourses, vector<vector<int>>& pt) { queue<int> q; vector<int> ans; vector<int > in(numCourses,0); vector<int> edge[numCourses]; for(int i=0;i<pt.size();i++) { in[pt[i][1]]++; edge[pt[i][0]].push_back(pt[i][1]); } for(int i=0;i<in.size();i++) if(in[i]==0)q.push(i); while(!q.empty()) { int tmp=q.front(); q.pop(); ans.push_back(tmp); for(int i=0;i<edge[tmp].size();i++) { int p=edge[tmp][i]; in[p]--; if(in[p]==0) q.push(p); } } return ans.size()==numCourses; } };