-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTopological Sort.cpp
More file actions
38 lines (30 loc) · 744 Bytes
/
Topological Sort.cpp
File metadata and controls
38 lines (30 loc) · 744 Bytes
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
int n; // number of vertices
vector<vector<int>> adj; // adjacency list of graph
vector<bool> visited;
vector<int> ans;
void dfs(int v) {
visited[v] = true;
for (int u : adj[v]) {
if (!visited[u])
dfs(u);
}
ans.push_back(v);
}
void topological_sort() {
visited.assign(n, false);
ans.clear();
for (int i = 0; i < n; ++i) {
if (!visited[i])
dfs(i);
}
reverse(ans.begin(), ans.end());
}
int32_t main()
{
//inputs and pass to topological_sort()
}
/*
-----------------------------------------------------------------------------------------------------------------------
Problem : https://www.codechef.com/COOK105A/problems/DINCPATH
Solution can be found beside
*/