-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopo_sort.cpp
81 lines (73 loc) · 1.26 KB
/
topo_sort.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<bits/stdc++.h>
#define sz 100
using namespace std;
vector<int> T;
//int n;
bool adj[sz][sz];
void topo_sort(int n)
{
int t[n], in_degree[n] = {0}, k =0, i, j;
bool vis[n];
queue<int> q;
for(int i =0; i<n; i++)
{
for(int j =0 ; j<n; j++)
{
if(adj[i][j])
in_degree[j] = in_degree[j]+1;
}
}
for(i=0; i<n; i++)
{
if(in_degree[i]==0)
{
q.push(i);
vis[i] = true;
}
}
while(!q.empty())
{
int vertex = q.front();
q.pop();
T.push_back(vertex);
for(j=0; j<n; j++)
{
if(adj[vertex][j]==true && vis[j] == false)
{
in_degree[j] = in_degree[j] -1;
if(in_degree[j]==0)
{
q.push(j);
vis[j] = true;
}
}
}
}
}
int main()
{
int e, n, i, j, x, y;
cin>>n>>e;
for(i=0; i<e; i++)
{
cin>>x>>y;
adj[x][y] = true;
}
topo_sort(n);
for(i=0; i<n; i++)
cout<<T[i]<<" ";
return 0;
}
/*
6 10
0 1
0 3
1 2
1 3
2 3
2 5
2 4
3 4
3 5
4 5
*/