-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path100000623-00.cpp
66 lines (60 loc) · 1.36 KB
/
100000623-00.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* hint:
* 本题必须用栈,而不能是队列,不然不能 AC
*/
#include <cstdio>
#include <vector>
#include <stack>
#include <cstring>
#define MAXN 55
using namespace std;
int n;
bool graph[MAXN][MAXN];
int in_degree[MAXN];
int ans[MAXN];
bool topological_sort()
{
// step 0: initialize
int num = 0;
stack<int> q;
// step 1: push vertexes whose in-degrees are 0
for (int i = 0; i < n; i++)
if (in_degree[i] == 0)
q.push(i);
// step 2: bfs
while (q.size())
{
int u = q.top();
q.pop();
ans[num++] = u;
for (int v = 0; v < n; v++)
if (graph[u][v])
{
in_degree[v]--;
if (in_degree[v] == 0)
q.push(v);
}
}
if (num == n) return true;
else return false;
}
int main()
{
while (scanf("%d", &n) != EOF && n != 0)
{
memset(in_degree, 0, sizeof(in_degree));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
scanf("%d", &graph[i][j]);
if (graph[i][j] == true)
in_degree[j]++;
}
if (topological_sort())
for (int i = 0; i < n; i++)
printf(i == n - 1 ? "%d \n" : "%d ", ans[i]);
else
printf("ERROR\n");
}
return 0;
}