forked from abhishekprog0/Linked-List
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_topological.cpp
More file actions
63 lines (50 loc) · 1.62 KB
/
Copy pathqueue_topological.cpp
File metadata and controls
63 lines (50 loc) · 1.62 KB
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
#include <iostream>
#include <vector>
#include <queue>
std::vector <bool> visited;
std::vector <int> ans, indegree;
void topological(const std::vector <std::vector <int> >& graph, std::queue <int> q) {
while(!q.empty()) {
// pop the front vertex and add it to ans
int u = q.front(); q.pop();
visited[u] = true;
ans.push_back(u);
// For all the non visited neighboring vertices of u, decrement
// their corresponding indegree.
for(size_t v = 0; v < graph[u].size(); v++) {
if(!visited[graph[u][v]]) {
indegree[graph[u][v]]--;
// if indegree of any vertex becomes 0, push it in the queue.
if(indegree[graph[u][v]] == 0) {
q.push(graph[u][v]);
visited[graph[u][v]] = true;
}
}
}
}
}
int main() {
// number of vertices in the graph.
int n, m, x, y;
std::cin >> n >> m;
std::vector <std::vector <int> > graph(n);
visited = std::vector <bool> (n, false);
indegree = std::vector <int> (n, 0);
// 1-based indexing assumed
for(size_t i = 0; i < m; i++) {
std::cin >> x >> y;
graph[x-1].push_back(y-1);
indegree[y-1]++;
}
// insert all the vertices in the queue that has indegree 0
std::queue <int> q;
for(size_t i = 0; i < n; i++) {
if(indegree[i] == 0)
q.push(i);
}
topological(graph, q);
for(size_t i = 0; i < ans.size(); i++)
std::cout << ans[i] << ' ';
std::cout << std::endl;
return 0;
}