for(int i = 0; i < prerequisites.size(); ++i){ int ready = prerequisites[i].first; int pre = prerequisites[i].second; if (matrix[pre][ready] == 0) indegree[ready]++; //duplicate case matrix[pre][ready] = 1; }
int count = 0; queue<int> que; for (int i = 0; i < indegree.size(); ++i) { if (indegree[i] == 0) que.push(i); }
while(!que.empty()){ int course = que.front(); que.pop(); count++; for (int i = 0; i < numCourses; ++i) { if (matrix[course][i] != 0) { indegree[i]--; if(indegree[i] == 0) que.push(i); } } }
for(int i = 0; i < prerequisites.size(); ++i){ int ready = prerequisites[i].first; int pre = prerequisites[i].second; if (matrix[pre][ready] == 0) indegree[ready]++; //duplicate case matrix[pre][ready] = 1; }
int count = 0; queue<int> que; for (int i = 0; i < indegree.size(); ++i) { if (indegree[i] == 0) que.push(i); }
while(!que.empty()){ int course = que.front(); que.pop(); ret.push_back(course); count++; for (int i = 0; i < numCourses; ++i) { if (matrix[course][i] != 0) { indegree[i]--; if(indegree[i] == 0) que.push(i); } } } if(ret.size() != numCourses) ret.clear(); return ret; } };