题目描述
代码及其实验结果
public class Solution {
public bool CanFinish(int numCourses, int[][] prerequisites) {
int [] inDegrees = new int[numCourses];
for (int i = 0; i < prerequisites.Length; i++) {
int []edge = prerequisites[i];
int v2 = edge[1];
inDegrees[v2] += 1;
}
Queue<int> queue = new Queue<int>();
for (int i = 0; i < numCourses; i++) {
if (inDegrees[i] == 0) {
queue.Enqueue(i);
}
}
while (queue.Count!=0) {
int v1 = queue.Dequeue();
for (int i = 0; i < prerequisites.Length; i++) {
[]edge = prerequisites[i];
if (edge[0] == v1) {
int v2 = edge[1];
inDegrees[v2] -= 1;
if (inDegrees[v2] == 0) {
queue.Enqueue(v2);
}
}
}
}
for (int i = 0; i < numCourses; i++) {
if (inDegrees[i] != 0) {
return false;
}
}
return true;
}
}