-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscoQ2.c
More file actions
37 lines (30 loc) · 840 Bytes
/
Copy pathdiscoQ2.c
File metadata and controls
37 lines (30 loc) · 840 Bytes
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
#include <stdio.h>
#define MAX 100
int main() {
int N;
scanf("%d", &N);
int mat[MAX][MAX];
int indeg[MAX] = {0}, order[MAX], front = 0, rear = 0, queue[MAX];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) {
scanf("%d", &mat[i][j]);
if (mat[i][j]) indeg[j]++;
}
// enqueue all zero indegree nodes
for (int i = 0; i < N; i++)
if (indeg[i] == 0) queue[rear++] = i;
int idx = 0;
while (front < rear) {
int u = queue[front++];
order[idx++] = u;
for (int v = 0; v < N; v++) {
if (mat[u][v]) {
indeg[v]--;
if (indeg[v] == 0) queue[rear++] = v;
}
}
}
for (int i = 0; i < N; i++)
printf("%d%c", order[i], i == N-1 ? '\n' : ' ');
return 0;
}