-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN2252.cpp
52 lines (49 loc) · 1.12 KB
/
N2252.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
# include <stdio.h>
# include <vector>
# include <queue>
# include <tuple>
# include <stdlib.h>
using namespace std;
vector<vector<int> > T;
vector<bool> visited;
queue<int> myQ;
int *pointed;
void topoSort();
int main(){
int n,m,node1,node2;
scanf("%d %d",&n,&m);
pointed = (int *)malloc(sizeof(int)*(n+1));
for(int i = 0; i<=n;i++)
pointed[i] = 0;
T.resize(n+1);
visited.assign(n+1,false);
for(int i = 0;i<m;i++){
scanf("%d %d",&node1,&node2);
T[node1].push_back(node2);
pointed[node2] += 1;
}
for(int i = 1;i<T.size();i++)
sort(T[i].begin(),T[i].end());
for(int i = 1;i<=n;i++){
if(pointed[i] == 0){
myQ.push(i);
visited[i] = true;
}
}
topoSort();
}
void topoSort(){
int front,next;
while(!myQ.empty()){
front = myQ.front();
myQ.pop();
printf("%d ",front);
for(int i = 0;i<T[front].size();i++){
next = T[front][i];
pointed[next] -= 1;
if(pointed[next] == 0){
myQ.push(next);
}
}
}
}