Topological Sorting_2
#include<bits/stdc++.h> using namespace std; long long visited[100]; vector <int> grp[200]; map<int,int>vis; stack<int>st; int n; int topologicalSortUtill(int v) { vis[v] = 1; int l=grp[v].size(); int u; for(int i=0;i<l;i++) { u=grp[v][i]; if (!vis[u]) topologicalSortUtill(u); } st.push(v); } int topologicalSort() { for(int i=0;i<n;i++) vis[i]=0; for(int i=0;i<n;i++) if(vis[i]==0) topologicalSortUtill(i); while(st.empty()==0) { cout<<st.top()<<" "; st.pop(); } } int main() { int m,i,j,a,b; cin>>n>>m; for(i=1; i<=m; i++) { cin>>a>>b; grp[a].push_back(b); } //for(i=0;i<n;i++) topologicalSort(); return 0; } /* 6 6 5 2 5 0 4 0 4 1 2 3 3 1 */