π One-stop destination for all your technical interview Preparation π
class Solution {
void dfs(int node, vector<int> adj[], vector<int>& vis, stack<int>& st)
{
vis[node] = 1;
for (auto it : adj[node]) {
if (!vis[it]) {
dfs(it, adj, vis, st);
}
}
st.push(node);
}
public:
// Function to return list containing vertices in Topological order.
vector<int> topoSort(int V, vector<int> adj[])
{
vector<int> vis(V, 0);
stack<int> st;
for (int i = 0; i < V; i++) {
if (!vis[i]) {
dfs(i, adj, vis, st);
}
}
vector<int> topo;
while (!st.empty()) {
topo.push_back(st.top());
st.pop();
}
return topo;
}
};