-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopological Sort (Matrix).cpp
69 lines (66 loc) · 1.33 KB
/
Topological Sort (Matrix).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
#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<utility>
#include<iomanip>
#include<queue>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define PB push_back
#define pi 3.1415926535897932384626433832795
int n,order[120][120],indegree[120],taken[120];
void topsort()
{
vector<int>task;
int i,j,k;
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
if(!taken[j] && !indegree[j])
{
taken[j]=1;
task.PB(j);
for(k=1; k<=n; k++)
if(order[j][k])
--indegree[k];
break;
}
}
}
cout<<task[0];
for(i=1; i<n; i++)
cout<<" "<<task[i];
cout<<endl;
task.clear();
}
int main()
{
int a,b,i,m;
while(cin>>n>>m)
{
if(n==0 && m==0) break;
for(i=1; i<=m; i++)
{
cin>>a>>b;
order[a][b]=1;
indegree[b]++;
}
topsort();
clr(order);
clr(indegree);
clr(taken);
}
return 0;
}