-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourse-schedule.c
More file actions
53 lines (39 loc) · 1.28 KB
/
course-schedule.c
File metadata and controls
53 lines (39 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
bool dfs(int curr, int** graph, int* g_col, int* visited){
if(visited[curr] == 1)
return false;
if(visited[curr] == 2)
return true;
visited[curr] = 1;
for(int i = 0; i < g_col[curr]; i++){
if(dfs(graph[curr][i], graph, g_col, visited) == false)
return false;
}
visited[curr] = 2;
return true;
}
bool canFinish(int numCourses, int** prerequisites, int prerequisitesSize, int* prerequisitesColSize) {
int visited[numCourses];
int g_col[numCourses];
int* graph[numCourses];
for(int i = 0; i < numCourses; i++){
graph[i] = (int*)malloc(numCourses * sizeof(int));
memset(graph[i], 0, numCourses * sizeof(int));
}
memset(g_col, 0, numCourses * sizeof(int));
memset(visited, 0, numCourses * sizeof(int));
for(int i = 0; i < prerequisitesSize; i++){
int course = prerequisites[i][0];
int pre = prerequisites[i][1];
graph[pre][g_col[pre]++] = course;
}
for(int i = 0; i < numCourses; i++){
if(dfs(i, graph, g_col, visited) == false){
for(int j = 0; j < numCourses; j++)
free(graph[j]);
return false;
}
}
for(int j = 0; j < numCourses; j++)
free(graph[j]);
return true;
}